Search Unity

Make camera smoothly follow a moving object that changes speed

Discussion in 'Scripting' started by roger0, Feb 18, 2018.

  1. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I am building a flight simulator and I want the camera to smoothly follow the aircraft. I can do this with lerp or slerp but the problem is if the aircraft changes speed such as go faster the camera starts to lag behind.

    I can fix this by making the camera follow speed faster but it will loose its smoothness and be like a parented camera.

    Another way to fix it is to adjust the lerp speed so when the aircraft goes faster the camera speed will go faster as well. But its tedious and values need to be precisely adjusted for each aircraft.

    Is there a simpler way of doing this so the camera more of less stays in the same position and keeps a smooth follow motion while the aircraft is speeding up or down?
     
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This is basically the exact problem SmoothDamp tries to solve.
     
  4. Deleted User

    Deleted User

    Guest

    Smooth damp example:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Utility : MonoBehaviour {
    7.  
    8.  
    9.     private Vector3 m_refPos;
    10.     private Vector3 m_refRot;
    11.  
    12.  
    13.     void Update () {
    14.  
    15.         // EXAMPLE FOR POSITION
    16.  
    17.         // Smooth out the reference to have smoother steps and without gittering
    18.         m_refPos *= Time.smoothDeltaTime;
    19.         this.transform.position = Vector3.SmoothDamp( this.transform.position, new Vector3( 0f, 2f, 0f ), ref m_refPos, 0.2f );
    20.  
    21.         // EXAMPLE FOR ROTATION
    22.         this.transform.rotation = SmoothDampRotation( this.transform.rotation, Quaternion.Euler( Vector3.up * 90f ), ref m_refRot, 0.2f );
    23.  
    24.     }
    25.  
    26.     public static Quaternion SmoothDampRotation( Quaternion ObjectToRotate, Quaternion WantedRotation, ref Vector3 SavedSmoothSteps, float RoationSpeed )
    27.     {
    28.  
    29.         var euler = ObjectToRotate.eulerAngles;
    30.         var eulerTarget = WantedRotation.eulerAngles;
    31.  
    32.         // Smooth out the reference to have smoother steps and without gittering
    33.         SavedSmoothSteps *= Time.smoothDeltaTime;
    34.  
    35.         euler.x = Mathf.SmoothDampAngle( euler.x, eulerTarget.x, ref SavedSmoothSteps.x, RoationSpeed );
    36.         euler.y = Mathf.SmoothDampAngle( euler.y, eulerTarget.y, ref SavedSmoothSteps.y, RoationSpeed );
    37.         euler.z = Mathf.SmoothDampAngle( euler.z, eulerTarget.z, ref SavedSmoothSteps.z, RoationSpeed );
    38.  
    39.         return Quaternion.Euler( euler );
    40.  
    41.     }
    42. }
    43.