Camera distance from player

Hi guys, I have a simple camera script set to follow target’s (player) position. I want the camera to stay at a more or less fixed distance from the player but at the moment the faster the player is moving the farther the camera is behind the player.
Any advice?

Camera m_MainCamera;

	public Transform target;
	public float turnSpeed = 4.0f;
	public float minCameraDist = 2.0f;

	public float interpolation = 10.0f;    

	bikeControllerv3 player;

	void Start() {

		m_MainCamera = Camera.main;
		m_MainCamera.enabled = true;

		player = FindObjectOfType<bikeControllerv3>();
	}

	void FixedUpdate() {

		float dist = Vector3.Distance (transform.position, target.position);

		Vector3 position = this.transform.position;
		position.y = Mathf.Lerp(this.transform.position.y, target.transform.position.y, interpolation);
		position.x = Mathf.Lerp(this.transform.position.x, target.transform.position.x, interpolation);
		position.z = Mathf.Lerp(this.transform.position.z, target.transform.position.z, interpolation);

		this.transform.position = position;

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

	}

Good day.

You have now a system that interpolates the position of the camera and the player by using LErp function.

Lerp function have 3 elements, positionA, positionB, and the “t” factor. The “t” factor is used for the interpolation from A to B, and is a value from 0 to 1.

If you set t=0, the result will be the PositionA, if you set t=1 the result will be positionB, if you set t=0.5f , the result will be exactly the mid position between A and B.

So, if you want a “speed effect”, you get it by “playing” with “t” element. When the player speed is low, make the “t” be something like 0.2 or 0.3, and when the speed is high, make the “t” something like 0.7 or 0.8.

This way, when speed is low, the camera follows the player very “fast”, and when the speed is hight, the camera “reacts slower”.

Try it!

For better results, mae the “t” value dependant of the speed, something like:

t = 0.1*Playerspeed BUT BE CAREFULL, “t” MUST BE A VALUE FROM 0 TO 1.

Bye!!