However, as the target (my spaceship) moves forward, the camera shakes unpleasantly, I believe due to the Lerp function. Is there a way to avoid this and just have the camera move the same way as the spaceship does? Thanks.
// A simple smooth follow camera,
// that follows the targets forward direction
var target : Transform;
var smooth = 0.3;
var height = 0;
var distance = 5.0;
private var yVelocity = 0.0;
function Update () {
// Damp angle from current y-angle towards target y-angle
var yAngle : float = Mathf.SmoothDampAngle(transform.eulerAngles.y,
target.eulerAngles.y, yVelocity, smooth);
// Position at the target
var position : Vector3 = target.position;
// Then offset by distance behind the new angle
position += Quaternion.Euler(0+height, yAngle, 0) * Vector3 (0, 0, -distance);
// Apply the position
transform.position = position;
Why don’t you use Unity 5’s built in Smooth Follow script from the docs? It has no lagging issues for a powerful PC, but if you have issues, use FixedUpdate() instead of Update().
var target : Transform;
var smoothTime = 0.3;
private var velocity = Vector3.zero;
function Update ()
{
// Define a target position above and behind the target transform
var targetPosition : Vector3 = target.TransformPoint(Vector3(0, 5, -10));
// Smoothly move the camera towards that target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, velocity, smoothTime);
}
It will not work if you have another script to control camera movement of ANY kind, so this is best used for a 2D game.