- Home /
Aligning objects to a grid with an offset
Currently the way I offset my objects is by adding a value to them at the end of the calculation of where their grid position is. Before all methods
public float gridDistance = 1f;
In the Start() method
gridSize = 1f / gridDistance;
In the Update() method
gridPosition = new Vector3(
Mathf.Round(transform.position.x * gridSize) * gridDistance,
Mathf.Round(transform.position.y * gridSize) * gridDistance,
Mathf.Round(transform.position.z * gridSize) * gridDistance);
And then if I want to offset it I'll do this
gridPosition = new Vector3(
Mathf.Round(newPos.x * gridSize) * gridDistance + gridDistance / 2f,
Mathf.Round(newPos.y * gridSize) * gridDistance,
Mathf.Round(newPos.z * gridSize) * gridDistance + gridDistance / 2f);
However, I would like the objects to round to an offset grid, rather than one grid and then offset.
Crude but effective illustration:
See how my cursor is in the middle of the screen, but the pillar is being placed on the far top left? That's because It's rounding my cursor point to a non-offset grid, and then adding like an offset to the final product. It would be great if the pillar's position would just round to where my cursor is, just on an offset grid.
Your answer
![](https://koobas.hobune.stream/wayback/20220612160832im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Snapping to grid with grid size of <= 1 1 Answer
Unity Position Error? 1 Answer
Having trouble adding offsets to my grid 1 Answer
Sprite object has unwanted offset to the grid 0 Answers
Rounding Errors when snapping to grid 2 Answers