Turing with Rigidbody.MoveRotation() is really janky

I’m working on a player movement controller, but when I try to turn it, it turns really jittery.

        float sidewaysTurn = Input.GetAxis("Mouse X");
        Vector3 rot = new Vector3(0, turn_speed * sidewaysTurn, 0);
        Quaternion rot_qauternion = Quaternion.Euler(rot);
        rb.MoveRotation(rb.rotation * rot_qauternion);

I also have a quick question regarding MovePosition. Is it possible to calculate velocity when the rigidbody is not kinematic?

What is jittery? The rigidbody? The camera?

You are using unfiltered mouse input which can often be a bit all over the place, depending on the mouse. You may want to just use GetAxisRaw to only find out the direction, and have sensitivity settings to modify the turning speed.

  • Rigidbody calculates and reads .velocity if the .isKinematic is false.

  • You should not mix MoveRotation/MovePosition with setting .velocity but reading .velocity from prior frame should be okay.

  • You should not mix AddTorque/AddForce and setting .velocity either.

  • Use Rigidbody’s .position instead of Transform’s .position if working with physics movements.

  • Use FixedUpdate for all your physics movements and calculations; the consistent delta time is important to avoid jitter.

  • Use Update for all your controller input polling or events; the input system only updates state once per Update and reading it in FixedUpdate might end up getting inconsistent results. Cache your desired movements to member variables that you can then use in subsequent FixedUpdate calls.

1 Like

I did this. Everything is in the fixed update, and I’m using MovePosition. Velocity is only read if is kinematic is true. When it’s false, it’s always read as 0.

Because your rigidbody isn’t kinematic you should place your code in Update.

If you place the code in FixedUpdate then the rotation won’t be smooth because FixedUpdate typically runs at a lower frame rate than Update. If your rigidbody was kinematic then it would be okay in FixedUpdate because MoveRotation on a kinematic rigidbody is interpolated to help keep it smooth.

K, I’ll try this. I can tell it’s the rotation and not the mouse, because looking at the inspector, the rotations are not smoothly changing— they jump.

I tried it in both. The rotation is jittery no matter what functions I put it in.

That’s… a really terrible way to deduce that, sorry. Have you actually debugged what the axis values from the mouse you’re getting are?

rb.velocity will be zero if you’re moving your object with MovePosition.

1 Like

Ok, I decided to switch and use CharacterController and program my own physics. It seems to be working a lot better!

1 Like

Another possibility for the janky rigidbody movement was because you hadn’t frozen the rigidbody’s rotation axis.