Car camera and annoying movements

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 :slight_smile:

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 ?

My first thought is to up the height at rotation damping, to like 2 or 3 and see how that works. More damping = less jittery camera. As far as the camera swapping sides, it is trying to stay behind the vehicle, so that is pretty normal. One of the ideas that I was toying with was to have a follow camera that actually followed the target, rather then using the target’s rotation to calculate it’s position. That idea sounds better for what you are doing.

I developed a decent camera setup in another thread if you want to have a look at it.

http://forum.unity3d.com/threads/76973-Car-Veiw-script-problem

Fail … height or rotation dumping does nothing better :frowning: I’ve tested your script but they do the same, every time when car goes “down” camera is rotating 180 degrees.

I don’t have idea how to block this rotation … in fact I don’t now how to convert Y car rotation into X-Z cam rotation without this “slides” … cam has to be always behind the car :frowning: