- Home /
How do you get the tile on which a gameObject is standing on Isometric Tilemap ?
Hi everybody !
I'm trying to use the isometric z as y tilemap and create a character on it. I want that the character can snap on the grid case exactly like the tile. But... It work in a way that i can't understand.
Why do they have different position ? How can i get the tile (and it position) on the gameObject is ? I'm so confuse right now.
Can someone (maybe) explain me, how do we create a lambda character and make it move on the grid ? (with taking the tile's position where he is standing on ?)
Thank you for reading,
Answer by z_orochii · Feb 06, 2019 at 06:20 AM
If you know the exact grid position (the one that shows at the first screen you posted), you can use GridLayout.CellToWorld to, as the method name suggests, convert a cell position into a world position. This one receives a Vector3Int with the position relative to the grid.
In order to use CellToWorld you must have a reference to the Grid object (like the one tilemaps use). And if you want to know the current (closest) cell where an object in the world is, you can use the opposite method: GridLayout.WorldToCell. This one receives as parameter a Vector3 corresponding to a point in the world space.
Hope it helps!
Thank you very much. Actually, it work perfectly with this kind of code :
Vector3Int lPos = grid.WorldToCell(player.transform.position);
Debug.Log("name : " + tilemap.GetTile(lPos).name + " & position : " + lPos);
Any way to bump up this solution? It's perfect, easy to implement, easy to understand, complete and works. But it is VERY hard to find this info through google.
Is there a way to sense if a tile is empty? If I do the method you provided whenever I move my character to an empty tile it gives me an error.
If you're using Descalium's code, just add a check for when the result from GetTile gives you null before asking for the tile name.
WorldToCell and/or CellToWorld shouldn't give you any issue with empty tiles, as it's just a conversion from world to grid. It doesn't use tiles or even the tilemap at any point.
@z_orochii Yea the problem is that its not giving me null, its giving me an error. I tried checking for null but that didn't work. I'm pretty new to Unity so I don't know how to do this. Also is there a way to check for the name of an adjacent tile? Edit: I'm also having trouble using the tile name in if statements. if (TileMap.GetTile(lPos).name == Grass) { // script here }