- Home /
How to make Smoothfollow align with targets local Z rotation?
So I want the camera to smoothly stay behind an aircraft, meaning it will lerp to its target rotation and position (with distance/height offset) over time. I know it has been asked like a thousand times, but I always get a camera movement jitter when the physics based jet moves and rotates. This is the camera-script:
public Transform target;
public float distance = 6f;
public float height = 1.5f;
[Range(0f, 1f)]
public float lerpSpeed = 0.7f;
void LateUpdate(){
transform.position = Vector3.Lerp(transform.position, target.position + (target.forward * -distance) + (target.up * height), Time.deltaTime * lerpSpeed * 60f);
transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, Time.deltaTime * lerpSpeed * 60f);
}
The standard SmoothFollow is not causing any jitter, however it follows the target at global XZ-plane, meaning it does not rotate when target rotates around its local z-axis (transform.LookAt aswell).
How can this be achieved?
Answer by MalachiteBR · Oct 04, 2017 at 10:10 PM
Why dont you use transform.lookat instead of rotation ?
I think that even if you dont lerp it, it will look ok because your jet is traveling at high speed so its movements will not be sooo fast...
Tried that before and didnt worked, but the problem is the cameras position that jitters, and LookAt will rotate the cam on local xy, not local xyz.
try this:
void Update(){
Vector3 dir = target - transform.position;
dir.y = 0; // keep the direction strictly horizontal
Quaternion rot = Quaternion.LookRotation(dir);
// slerp to the desired rotation over time
transform.rotation = Quaternion.Slerp(transform.rotation, rot, speed * Time.deltaTime);
}