Search Unity

Jerky 3rd Person Camera Following Movement and Rotation

Discussion in 'Scripting' started by siddharth3322, Jan 1, 2020.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    For my 3d game, I have attached a camera to follow my player object in position and rotation change.

    But overall I was getting jerky camera follow behavior. I double-checked by stopping the camera follow with player movement behavior. If I don't attach camera to a player then the player has smooth movement and rotation but as I started to follow the player, the screen is started stuttering.

    Here is the code that I have used to follow the player object in rotation and position:

    Code (CSharp):
    1. public class CameraFollow : MonoBehaviour
    2. {
    3.     Vector3 offset;
    4.     Quaternion offsetRotation;
    5.     //
    6.     public Transform target;
    7.     [SerializeField] float damping = 1;
    8.  
    9.     void Start()
    10.     {
    11.         offset = transform.position - target.position;
    12.         offsetRotation = transform.rotation;
    13.     }
    14.  
    15.     void LateUpdate()
    16.     {
    17.        
    18.         if (!target)
    19.             return;
    20.  
    21.  
    22.         float currentAngle = transform.eulerAngles.y;
    23.         float desiredAngle = target.eulerAngles.y;
    24.         float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime * damping);
    25.  
    26.         Quaternion rotation = Quaternion.Euler(0f, angle, 0f);
    27.         transform.position = target.position + (rotation * offset);
    28.  
    29.         transform.LookAt(target);
    30.         transform.rotation *= Quaternion.Euler(-10f, 0f, 0f);
    31.     }
    32. }
    As per my thinking, transform.LookAt statement creating a problem in smooth motion. Please share your suggestion to make overall feel smooth.
     
  2. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933

    Why not make the camera child of the player ? It will follow the player automatic.
     
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Do you think this is an optimal solution?
    Then why there are so many ways exist for the camera to follow scripts!!

    Even Unity within their demo projects used camera follow script rather than making its child.
     
  4. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Player movement is on Update?
     
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I was taking input from the Update method and doing actual player movement in FixedUpdate using this way.

    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         playerRotation.y = horizontalMovement;
    4.         playerMovement.z = forwardMovement;
    5.         myRigidbody.MovePosition(myRigidbody.position + transform.TransformDirection(playerMovement) * movementSpeed * Time.fixedDeltaTime);
    6.  
    7.         Quaternion deltaRotation = Quaternion.Euler(playerRotation * rotationSpeed * Time.fixedDeltaTime);
    8.         myRigidbody.MoveRotation(myRigidbody.rotation * deltaRotation);
    9.     }
     
  6. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Hmm then try to use this
    Code (CSharp):
    1. public float smoothSpeed = 0.2f;
    2.  
    3. //in LateUpdate
    4. Vector3 dest = target.position + (rotation * offset);
    5. transform.position = Vector3.Lerp(transform.position, dest, smoothSpeed);
     
  7. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Also, I have tested that if I stop camera follow then player doing movement and rotation properly it does not have any jittery movement.

    So that I am concerning about Camera Follow related issue.

    But let me try your given suggestion and apply.
     
  8. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I tested this and it gave little bit better output compare to before but rotation come too slow :(
    If I increase smoothness value from 0.2 to 0.5 then the same problem like before started.
     
  9. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181