- Home /
 
               Question by 
               Sondre-S · Feb 08, 2020 at 11:30 PM · 
                camera-movementcamera-lookcamera followcamera rotation  
              
 
              Smooth Follow cam; how to keep camera close, but still retain sideways lag/lerp
So, I wrote this fairly simple camera script , and it works well for what it does, but I would like to limit how far behind the player the camera can "lag" but still retain the "sideways lag".
I want the camera to stay with the player even at high speed, but still lag behind when the target turns.
I want it to behave more like this: https://youtu.be/MigtfNua7O4?t=33
I thought about using two different lerp values, but I'm not sure how to do that.
And, yes, I am aware of Cinemachine, but I want to code this myself..
 public Transform target;
 public float PosLerpSpeed= 5f;
 public float xOffset;
 public float heightOffset=3f;
 public float distanceOffset=8f;
 void FixedUpdate () {
     // define wanted camera position
     Vector3 offset=new Vector3(xOffset,heightOffset,-distanceOffset);
     Vector3 wantedCameraPosition= target.rotation * offset + target.position;
     
     // lerp towards wanted camera position
     Vector3 smoothedPos= Vector3.Lerp(transform.position,wantedCameraPosition,Time.deltaTime*PosLerpSpeed);
     transform.position = smoothedPos;
 
     // look at target
     transform.LookAt(target,target.up);
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Namey5 · Feb 09, 2020 at 08:51 AM
You're pretty much there; all you need to do is separate the movement from the offset, i.e.
 //With your other variables
 private Vector3 lastOffset = new Vector3 (0f, 0f, -1f);
 
 ...
 //Then inside your update function
 Vector3 offset = new Vector3 (xOffset, heightOffset, -distanceOffset);
 Vector3 wantedCameraPosition = target.rotation * offset;
      
 Vector3 smoothedPos = Vector3.Lerp (lastOffset, wantedCameraPosition, Time.deltaTime * PosLerpSpeed);
 transform.position = smoothedPos + target.position;
 //Remember to keep track of the last offset
 lastOffset = smoothedPos;
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                