- Home /
How am I supposed to use SetTile( ) on a GameObject's position if its x and y values aren't integers? ,
So, I have a GameObject at coordinates x = 0.5 y = 0.5, which is exactly centered on where a tile would be on that spot (I can compare it to the grid and see this) and I want to use SetTile() there. However, this function only accepts Vector3Int, not Vector3, which means the tile will have its position rounded up, and will end somewhere else. How do I get around this?
I could offset everything by .5, but that doesn't seem at all like the right choice (and even after doing that, I ran the game and saw I'd need to subtract -1 from the original coordinates' x and y axis, for some reason). Maybe changing the GameObject's sprite pivot would help, but that seems too tacky for me.
Any help is appreciated.
Answer by Bernardozomer · Oct 09, 2020 at 12:16 PM
Solved it, though I'd appreciate a better solution if anyone has that to offer.
After finding this question here, I tried fiddling around with Mathf.RoundToInt instead of (int) and adding/subtracting from the original position, which eventually got me to something like this:
Vector3Int newPos = new Vector3Int(
Mathf.RoundToInt(ogPos.x - 0.5f),
Mathf.RoundToInt(ogPos.y - 0.5f),
(int)ground.transform.position.z);
tilemap.SetTile(newPos, tile);
It works for me, so I thought I'd share.