Hi
I’ve got car camera script from one of tutorials.
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 15.0f;
// the height we want the camera to be above the target
public float height = 0.5f;
// How much we
public float heightDamping = 1.0f;
public float rotationDamping = 1.0f;
// Place the script in the Camera-Control group in the component menu
void LateUpdate ()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y - 180;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= transform.position * Vector3.forward * distance;
// Set the height of the camera
transform.Translate(0, currentHeight - 1.1f, 0);
// Always look at the target
transform.LookAt( new Vector3(target.position.x, target.position.y + 4, target.position.z));
}
Everything works well … when the car is moving on flat terrain. Troubles starts when car is going to the “mountains”.
When hi goes up, camera is behind him … ok.
But when he goes down, camera does 180 degree movement and go in front of the car. This is maybe a good solution for camera bug ( in this case camera will never collide with terrain ) but i don’t want this
The problem is here:
transform.position -= transform.position * Vector3.forward * distance;
… in fact “Vector3.forward * distance” does nothing … only sets distance from car.
Any suggestions ?