Camera follow issues

I have a game where the camera’s rotation is fixed, but i want it to follow the player. However, the player’s rotation changes. I’m having trouble writing a script to account for that. Here’s what I have:

var playerSpeed : float;
var playerChar : GameObject;

function Update () {
//transform.Translate(Vector3(Input.GetAxisRaw(“Horizontal”)Time.deltaTimeplayerSpeed,0,Input.GetAxisRaw(“Vertical”)Time.deltaTimeplayerSpeed));
if(Input.GetAxisRaw(“Horizontal”) != 0 Input.GetAxisRaw(“Vertical”) != 0) {
// transform.Translate(
} else if (Input.GetAxisRaw(“Horizontal”) != 0) {
// transform.Translate(Vector3(Time.deltaTimeplayerSpeed,0,Time.deltaTimeplayerSpeed*
} else if (Input.GetAxisRaw(“Vertical”) != 0) {
transform.Translate(Vector3(Time.deltaTimeplayerSpeedMathf.Sin(playerChar.transform.rotation.yMathf.Deg2Rad),0,Time.deltaTimeplayerSpeedMathf.Cos(playerChar.transform.rotation.yMathf.Deg2Rad)));
}
}

I’m only working with the vertical axis right now for simplicity. Thanks in advance.

The standard smooth follow camera does what you are asking for I think. Change off the damping to control how well the camera follows.

When I try standard smooth follow, I can’t get the camera to move

Is the “target” set to the object you want to follow?

Yes its set to the player. When I tried running it, the camera only moved forward, not backward or diagonal, even when i rotated the playerChar.

tinker with the dampening… it will do what you want.

Still no luck. I dont think its what I want. I want an alien swarm style camera.

try this:

public var distance = 20;
public var transform target;
function Update()
{
	// POSITION CAMERA:
	Vector3 position = target.position - (transform.rotation * Vector3.forward * distance);
}

untested