Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Camera freezes when interpolation mode is enabled in RigidBody

Discussion in 'Physics' started by Funtik63, Aug 26, 2023.

  1. Funtik63

    Funtik63

    Joined:
    Apr 29, 2022
    Posts:
    9
    I have a problem: when I add a Rigidbody to the player and turn on interpolation, then when I rotate the camera with the mouse, it starts to stutter. I am using the following code to enable a radio button:

    Code (CSharp):
    1. [SerializeField]
    2.     public float MouseSpeed = 200f;
    3.  
    4.     public Transform playerBody;
    5.  
    6.  
    7.     float xRotation = 0f;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.     }
    14.     private void LateUpdate()
    15.     {
    16.         float mouseX = Input.GetAxis("Mouse X") * MouseSpeed * Time.deltaTime;
    17.         float mouseY = Input.GetAxis("Mouse Y") * MouseSpeed * Time.deltaTime;
    18.  
    19.  
    20.  
    21.         xRotation -= mouseY;
    22.  
    23.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    24.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    25.  
    26.  
    27.         playerBody.Rotate(Vector3.up * mouseX);
    28.  
    29.     }
    Why might this be happening?
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,716
    This happens because both the rigidbody and your script are fighting for control over the transform's rotation.

    Interpolation works by taking the rigidbody's position in the previous frame and its current position, and picking an intermediate value between them based on the amount of time passed. Then the result is written to the transform's position, so the transform will sit in between two different physics states. Same thing applies to rotation.

    So if you want to rotate a rigidbody, rotate the rigidbody, not the transform. Keep in mind that transform.position and rigidbody.position are not the same thing, and will often have different values when interpolation is enabled.
     
    Last edited: Sep 3, 2023
  3. Funtik63

    Funtik63

    Joined:
    Apr 29, 2022
    Posts:
    9
    Thank you, I've been looking for an answer for so long, but it turned out to be so simple...