Smooth follow on all axis

Does anyone know of a script that is like the smooth follow script except it follows all axis? I’m making a aircraft game in third person, and I want the camera to stay behind the aircraft no matter which way it turns. I could of course parent the camera to the airplane but there would be no smoothing whatsoever.

I tried just duplicating the code inside of the smooth follow script and changing the areas where it references position.y and eularangles.y to the X axis instead. but it does not seem to be working.

If I’m understanding you properly (typed in browser):

var target : Transform;
var offset = -Vector3.forward;
var moveSpeed = 1.0f;
var rotSpeed = 360.0f;

function LateUpdate(){
  var wantedPos = target.position + target.rotation * offset;
  
  transform.position = Vector3.Lerp(transform.position, wantedPos, moveSpeed);
  transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotSpeed);
}

Hey, nice script. It comes close to what i’m looking for. Didn’t know it could be done with such little code. However, when I tried using it for my airplane, when the airplane rolls, the camera does not roll with it. Also, when the aircraft turns in a loop, the camera flips around. I want the camera to generally stay behind the aircraft at all times, with a smooth factor.

Also, what does f mean after 1.0f?

F means float, in C# you need that so that the language will recognize your number as a float type, but the f is not required in JavaScript.

After working with it for awhile, I was able to get a script that would not flip when the plane does a loop. Its a pretty short script. However, I ran into another problem. When the airplane is in flight, the camera shakes tremendously. How can I fix this?

Below is my script for the camera. The target is a gameObject behind the aircraft where I want the position of the camera to rest at.

var target : Transform;
var aircraft : Transform;
var speed = 5.0;

function LateUpdate() {

transform.position = Vector3.Lerp ( transform.position, target.position,Time.deltaTime * speed);

transform.rotation = transform.rotation = Quaternion.Lerp(transform.rotation,aircraft.rotation,speed*Time.deltaTime);          
}
1 Like

I just fixed it, had to set interpolate on the planes rigidbody to interpolate.