Camera movement speed relative to player - help me understand

Hi guys

I’m hoping someone can help me understand what is happening with my camera. I have a simple follow script on my camera and all it really does is:

transform.position = target.position

The issue I’m having is that I want the camera to maintain a constant distance from the player no matter the player’s movement speed, but at the moment the speed of the player dictates the distance (faster = farther away).


The camera has no movement speed setting so its simply trying to get to the player’s position at all times. Indeed, when the player comes to a stop the camera does catch up. Am I right in thinking that this is a result of the default settings of the physics engine? In other words the increased distance is the natural result of higher speed = more movement per frame?

I have tried compensating for the distance by adding a dynamic offset value to the camera’s Z axis (relative to player’s transform.forward) to make it move forward or backward depending on player speed but this is causing unwanted changes to how the camera behaves when rotating.

Can anyone suggest what I might get the camera to stay at the same distance from the player?
Do I need to parent the camera to the player? I would rather not do this as I will lose a lot of nice existing behaviour and have a new range of issues to solve but if it’s the only way then fine.
Can I tell the camera “this is the player’s velocity” so that it can somehow adjust its own velocity to match?

Full camera script:

public class FollowPlayer2: MonoBehaviour {

public Transform target;
public float turnSpeed = 2.8f;

float interpolation = 1.0f;

void FixedUpdate() {

   Vector3 targetPosition = target.transform.position;

   this.transform.position = Vector3.Lerp(this.transform.position, targetPosition, interpolation);

   transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, turnSpeed*Time.deltaTime);

}

}

As said by @Harinezumi the issue was that the camera movement was being done in FixedUpdate(). Simply changing the script so the camera movement is handled in LateUpdate() has completely fixed the issue. In fact, the only piece of information on the Unity Script API page for LateUpdate says the following: “…a follow camera should always be implemented in LateUpdate…”

In the FixedUpdate you’re saying that the camera position is linearly interpolated by 1f. That means, it finds the point in-between the camera’s current position and the target position and moving to that. The middle.
As you go faster, that distance gets bigger with each fixed frame update, because it’s fixed… right?
So just remove:

this.transform.position = Vector3.Lerp(this.transform.position, targetPosition, interpolation);

Does this lag occur when the player is going only in a straight line?

make the camera a child of the player.