- Home /
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;
}
Answer by robertbu · May 07, 2013 at 07:24 PM
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
Perfect ,I am such a dufus sometimes I already had a camera target at the centre of the car ;p SOmetimes the obvious needs to be pointed out lol ;p
Your answer
Follow this Question
Related Questions
limiting camera rotation 0 Answers
Set limits for camera to other axis 2D 1 Answer
limit camera vertical rotation 1 Answer
ORBIT CAMERA ZOOM LIMIT 0 Answers
Trying to even a randomized rotation 0 Answers