All the codes is below. I am using 3D Polygon colliders that cover the entire tile. Camera is orthographic.
Here is an album showing the issue. And it got weirder - most of the time I have the issue when hitting the right side of a tile or the top of a tile. Well in this example I set up that was supposed to show the issue when hitting the right side of a tile I had the problem hitting the left side of the tile and hitting the right side worked fine.
16 pixels per unit. Each tile is 16 pixels. The tilemap is a child of an object and that object is rotated 90 on x axis.
rayc is the vector of the raycast hit
rayc.x
rayc.y - this is 0 each time because I rotated the parent on the X axis 90 degrees
rayc.z
X - the x value returned from GetTileAtPosition(rayc)
Y - the y value returned from GetTileAtPosition(rayc)
fracX - the float x value returned from GetTileAtFracPosition(rayc)
fracY - the float y value returned from GetTileAtFracPosition(rayc)
Image 1 - Shows the setup before any raycasting. I will first raycast at (0,0)
Image 2 - After 5 hits on the right part of the tile, the tile is removed. I have a class that gives each tile health based on the tile ID and it's coordinates on the tilemap. So tile (0,0) has 5 health, so does tile (5,0) which will be hit next. The log shows 5 of the same message, I raycasted 5 times, hit the block 5 times, and then the block was set to a white tile and the map was rebuilt.
Image 3 - I am raycasting towards tile (5,0) and in the log you can see the rayc.x is 5, X is 4, fracX is 5. I use the X value, not the fracX value, when identifying the tile to remove health from. So this is not a debugger rounding error. You can see that I tried raycasting 58 times and still the tile was there.
Image 4 - Remember how I said there were two issues? Issue one was the GetTileAtPosition returning the wrong thing. Issue 2 was that 99% of the time the wrong thing is returned. Well around 150 raycasts later (you can see the number of collapsed logs at 178) I got the right coordinates 5 times and the tile was removed. So really 97% of the time I am getting the wrong value.
So there it is documented. And it is strange because this time the issue was hitting the left side of a tile, not hitting the right side of a tile. I did not test hitting the top or bottom of a tile.
The code below is what is used. You can see how I got all the Debog Logs. Also, you can see that the health section is at the bottom and I use the values from GetTileAtPosition and it returns the X value as int X and the Y value as int y.
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Raycast try");
Vector3 dir = mouseTemp - character.transform.position;
RaycastHit hit;
if (Physics.Raycast (character.transform.position, dir, out hit, 1000)) {
Debug.Log ("Raycast collision detected");
Vector3 rayc = hit.point;
int k = -1;
foreach (tk2dTileMap s in maps) {
Debug.Log (s.name);
k = k + 1;
int x;
int y;
float jx;
float jy;
if (s.GetTileAtPosition (rayc, out x, out y)) {
Debug.Log ("rayc x " + rayc.x);
Debug.Log ("rayc y " + rayc.y);
Debug.Log ("rayc z " + rayc.z);
if(s.GetTileFracAtPosition(rayc,out jx ,out jy)){
Debug.Log("X " + x+"\nY " + y);
Debug.Log("\nfracX " + jx + "\nfracY " + jy);
Debug.Log("\nfracY " + jy);
}
int health = tij [k].tilehealth [new Vector2 (x, y)] - 1;
if (tij [k].tilehealth [new Vector2 (x, y)] == 1) {
s.SetTile (x, y, 0, 57);
s.Build ();
}
if (tij [k].tilehealth [new Vector2 (x, y)] > 1) {
tij [k].tilehealth [new Vector2 (x, y)] = health;
}
}
}
}
}