RigidBody X and Y velocity is affecting Y rotation

I have this extremely simple code right here for a velocity based movement for a retro shooter clone. The mouse movement is locked to the Y axis of the player so there is no X camera rotation. When the X and Z velocities are applied independently there isn’t a problem, but when they are applied simultaneously, it rotates the Y axis of the character as if it was affecting the mouse look.
I believe it has a correlation with how I apply the velocity to the X and Z axis, with the line ```
theRB.velocity = (moveHorizontal + moveVertical) * moveSpeed;


PlayerMovement Code: 

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public Rigidbody theRB;
    public float moveSpeed = 5f;
    public Vector3 moveInput;
    private Vector2 mouseInput;
    public float mouseSensitivity = 10f;
   
    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
       
    }

  
    void Update()
    {

        //Movement
        moveInput = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        Vector3 moveHorizontal = transform.right * moveInput.x;
        Vector3 moveVertical = transform.forward * moveInput.z;
        theRB.velocity = (moveHorizontal + moveVertical) * moveSpeed;


        //Mouse Control
        mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), 0) * mouseSensitivity;
        transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y + mouseInput.x, 0);

       
    }
}

“In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour - use AddForce instead Do not set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical usage is where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity.”
It could be the problem, another thing don’t use Update() for physics, use FixedUpdate()
I didn’t understand if the Y rotation should be fixed or not, if yes use constraints to freeze the rotation

1 Like