- Home /
How to number tiles ?
I want to number tiles and when gameobject moves, the gameobject should know which tiles it's in. I dont want to use collider. How can I do that ?
This is my game area. For example tile numbers should increase left to right. and when gameobject starts to move tile to tile, it should know which tile it's in. For example if gameobject in tile in this image, it should know that it's in sixth tile
Answer by wesleywh · Jul 04, 2021 at 08:56 PM
Use vector positioning. Looks like its a grid so its always the same. If you use snap movement then its easy. It becomes even easier if each grid is a 1x1. Then have your lower left portion of the grid be at 0 x and 0 z. Then you know exactly where your at. So example if my tile is at x position 0 and z position 4 I know I'm in the first row 5th column (if starting at 0). You can always approximate these cells by doing a
Mathf.RoundToInt
For your x and z to guess the closest matching cell. No colliders needed. Hope this helps
Thanks for your answer. But there is one problem with this solution I think. The problem is when I want to execute a method when cube is right on a tile with if statement by equalize tile x and z position with cube x and z position, ıt never works. Because x and z positions never be same(ı think because of float part but I'm not sure) What should I do ? @wesleywh
Yes comparing Floats is a bad idea, they will more than likely never match. Comparing your x and z only AFTER you have rounded them to an int instead of a float.