int myX = (int)transform.position.x;

Can someone explain me why the following lines of code :

int myX = (int)transform.position.x;

int myZ = (int)transform.position.z;

Debug.Log("trxy= " + transform.position.x + ", " + transform.position.z + "   myxz= " + myX + ", " + myZ);

are tracing me those results :

trxz= 25, 12 myxz= 25, 11

trxz= 30, 13 myxz= 30, 12

but on 40 traces, several are ok, example :

trxy= 15, 27 myxz= 15, 27

trxy= 27, 21 myxz= 27, 21

i’m loosing time on that one !

It is likely the ToString() method for integers rounds to some decimal place, but the casting to an int truncates. So if the real value of transform.position.z was 11.99999999999999, the string would be 12, and the integer cast of that value would be 11. To get around the problem do:

 int myX = Mathf.RoundToInt(transform.position.x);
 int myZ = Mathf.RoundToInt(transform.position.z);