Limit cameras near range ?

Hi guys ,I am using a simple follow camera that always stays above the terrain ,and I am trying to stop the camera getting too close to my vehicle ,can anyone tell me why the code isn’t quite correct please ? (the terrain height works ,but I cannot seem to limit the near distance)

var target : Transform;

function FixedUpdate () {
	transform.position = Vector3.Lerp (transform.position, target.position + Vector3.up * 5, Time.deltaTime * 160 * Time.deltaTime);  
	transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position, target.up), Time.deltaTime * 2);
    	transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position)+5; 
    if (transform.position.x <=10) transform.position.x =10; 
	if (transform.position.z <=10) transform.position.z =10;
	}

On lines 7 & 8 you are limiting the global position of the camera. These positions have nothing to do with the target. Plus your limiting of 10 for x and z doesn’t mean a 10 unit following distance given the car may be at arbitrary angles. I could offer a code fix, but there is a much simpler solution.

  • Place an empty game object 10 units behind the car
  • Make the empty game object a child of the car
  • Use the empty game object as the ‘target’ above
  • Remove lines 7 & 8