3D Platformer Camera Question

I’m making a 3D platforming demo in Unity, and I’ve always struggled to get the correct behaviour from my camera.

I want similar behaviour to what I’d expect from a 3D platformer of old. i.e. Jak and Daxter, Sly Cooper etc.

I want the camera to:

  • always look at the player (or arbitrary point)
  • freely orbit about the player (or arbitary point)
  • stay a fixed distance from the player
  • only move when it has to

I have achieved the first by using a simple transform.LookAt(), the second I can manage on my own. It’s the second two I have trouble with.

What I mean by these last two points is that the camera should be pulled along behind the player but not necessarily always behind.

You could try this. It may need to be modified to fit your exact needs.

var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;

function Update () {
var wantedPosition = target.TransformPoint(0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);

if (smoothRotation) {
var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}

else transform.LookAt (target, target.up);
}

Add it to your camera and drag your character to the target variable.

Points 3 and 4 can be done by having the main camera float freely in world space (so no childing to the player character) and attaching a the SmoothLookAt script from the standard assets. This will allow you to set a follow distance thus giving you the fixed camera distance.