- Home /
Clamping rigidbody's Z position causing issues in the editor, when transform hits zero on Z axis
Ahoy,
I've got a player with a rigidbody and I'm moving it in FixedUpdate using:
Rb.velocity = new Vector3(Input.GetAxis("Horizontal") * speed, Rb.velocity.y, Input.GetAxis("Vertical") * zSpeed);
I'm also trying to restrict its Z position between 0 and 1 in this way:
Rb.position = new Vector3(Rb.position.x, Rb.position.y, Mathf.Clamp(Rb.position.z, 0, 1));
Everything seems to be working BUT!
The player's Z position goes haywire in the editor as soon as it reaches 0. The Z position jumps to anywhere from 0.xxx to 7.xxx and more but the player itself does not seem to be shifting along the Z axis, and as soon as I hold the "vertical" buttons it sorts itself out.
The code does what's intended but I'm wondering why this happens and if it's using resources for some useless calculations. Regards ;)