- Home /
How to Snap to Grid Cells
Hello,
I have a 2D collider grid that is 3.1w by 2.2h in whatever default unit Unity uses. It has square grids, and the collider's grid dimensions are 9 x 5 - so 9 squares across and 5 down. I want to be able to click on this collider and have objects snap to the middle of the squares. If my dimension is 3.1 by 2.2, I can't use Mathf to round to a whole integer to have objects snap to the middle since my collider does not have whole number dimensions nor is it obviously divisible by 9 and 5. How should I go about this?
Thanks!
It seams trivial to get the collider's center point, each collider has a property called "center", just use transform.TransformPoint on host gameobject to get the world position... But it feels like your use case is something like drag objects into the slots for which there can be some best practices you can find on the internet...
Your answer