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);
}
}