Rotate camera 90 degrees from target, and follow ... demo not working properly

Hello,

I’m creating a scene where you can walk a character along a path. I am trying to get the camera to follow the character at a 90 degree angle from his forward direction, which is constantly changing as he moves.

Here is a demo, use A and D to move) : Character Demo

This is the code that I have so far, which is attached to my camera, is:

var cameraYOffset : float = 10f;
var cameraDistance : float = 20f;

function LateUpdate () {
	if (target) {
        // Find the new camera position, 90deg right from target forward
		var rightPos : Vector3 = Vector3.Cross(Vector3.up, -target.forward) * cameraDistance;
	    rightPos.y += cameraYOffset;
	    transform.position = rightPos;
		
		// Now look at the target
		transform.LookAt(target);
	}
}

So this kind of works, but you’ll see as the character walks further along the path (towards the end mostly), the camera sort of stays still while the character walks off into the distance. I can’t figure out why this is happening. Wondering if anyone has any ideas…

Ryan

Try something like:

var cameraYOffset : float = 10f;
var cameraDistance : float = 20f;
 
function LateUpdate () {
  if (target) {
  // position self at target with same orientation
  transform.position = target.position;
  transform.rotation = target.rotation;

  //moves to the side of target and up
  transform.Translate(cameraDistance, cameraYOffset, 0);
 
  // Now look at the target
  transform.LookAt(target);
}
}

I still think it would be better to simply nest the camera on the target though.