- Home /
Mathf.Round inside method behaves different
Hi there, I have that weird problem that the Mathf.Round behaves differently when it's inside a method. I rewrote my code for moving a cube and wanted to make it a bit more readable. That's how this... transform.position = new Vector3(Mathf.Round(transform.position.x), transform.position.y, Mathf.Round(transform.position.z));
...became that: transform.position = VectorRound(transform.position);
Vector3 VectorRound(Vector3 vector) { return new Vector3(Mathf.Round(vector.x), vector.y, Mathf.Round(vector.x)); }
The first time I wrote the script (example 1), it worked perfectly fine, but when I moved those things to the method, my cubes are glitching around all over the place. It may be an rounding issue, even though these two examples are exactly the same in my eyes. Do I have to use the first solution, or is there a fix to the second one? Thanks in advance!
Answer by Zaeran · Oct 30, 2020 at 06:02 PM
In your method, you're assigning the rounded X component to the Z component of the vector. Changing it to vector.z should help.
Thank you so much, I should really be a lot more careful when writing stuff!