How to handle grid snapping when the object is greater than the grid cell width and height?

by JacketPotatoeFan   Last Updated August 13, 2019 13:13 PM

I want to be able to move objects on a grid always by what the grid cell size is, and objects bigger than the grid size should not overlap.

The cube in this GIF below is snapping how I want (1 x 1).

enter image description here

If I then change the size of the object, it doesn't snap to the grid quite how I would like. It snaps, but overlaps. I don't want an object to overlap the grid cell.

enter image description here

The snapping code is pretty straight forward and has been posted quite a lot and seems to work fine.

pos.x = Mathf.Round(hit.point.x + (hitNormal.x * objScale.x)) * gridSize;
pos.y = Mathf.Round(hit.point.y + (hitNormal.y * objScale.y)) * gridSize;
pos.z = Mathf.Round(hit.point.z + (hitNormal.z * objScale.z)) * gridSize;

I was thinking that I need to somehow set a pivot / anchor point, but I wouldn't want that to be one corner of the object, I was thinking somehow I could find the center of the object and work from there so it can snap correctly. Not sure if this is correct, or how to work that out.

Tags : unity grid

Related Questions

Updated July 29, 2018 14:13 PM

Updated June 28, 2018 07:13 AM

Updated June 18, 2018 10:13 AM

Updated August 03, 2017 17:13 PM

Updated September 14, 2018 02:13 AM