- Home /
How can i make the Unity2d grid use rounded numbers?
I'm new to unity and am creating a simple grid-based game to understand things better. in the game I have a "start position" tile that sets where my hero will start the level. however, I can't seem to get the tile to be located at a clean (1,2) kind of coordinate. instead, the location of the tile is always a long float out to several decimal places, like (-3.897622, 0.9561565). This is creating problems further down the road when I want to specific particular cell/tile locations. Is there a way to force unity to use Ints or rounded floats for its grid locations?
How do you place the tiles? By hand in the editor? If so, you can change the "Edit > Snap Settings" and hold CTRL key (or C$$anonymous$$D on $$anonymous$$ac) when moving your tiles.
thanks. yeah, i'm snapping to the grid that way and I've set the snap settings to whole values for x/y/z. The tiles still snap to odd values. maybe I've hit a bug...
The Snap settings is in fact a "step" setting. Set the position of the tile to a round value (like 0, 0, 0
), and when you hold CTRL key, the tile will be moved by 1 unit. If the initial position of the tile is something like 1.89756, 6.578996, 0.345663
, moving the object while holding CTRL will just move the tile by 1 unit 2.89756, 6.578996, 0.345663
gotcha thanks. This suggests that if i'm building off a grid, then the first tile/object I place in the seem is effectively how the grid is defined thereafter. is that right?
Answer by astrospacemoth · Nov 21, 2019 at 05:58 PM
I'm going to assume your tiles are NOT placed by hand in the editor, but rather at runtime. There is Mathf.Round which might work in this case. Try running your location value through that. https://docs.unity3d.com/ScriptReference/Mathf.Round.html
i'm placing them in the editor using grid snap no at runtime. things should just always snap to rounded values by default right? $$anonymous$$y grid anchors are all set to zero, grid settings for snap are all set to 1, etc.
Try holding Ctrl while moving the object in the editor.
Your answer
Follow this Question
Related Questions
Tilemap Grid Broken? 1 Answer
2D Tile Map Question 0 Answers
Hide tilemap grid 0 Answers
Why is 200x100 Tile Taking 3 100x100 Grid Spaces? 0 Answers
Grid and or Tile System 0 Answers