Car Camera Control Script

I’m following a tutorial for making a car game on unity as I need this for a uni project. The tutorial is in javascript and i’m translating it to C# as I go. I’ve did everything successful so far but the only thing that won’t work and I can’t figure it out is that the camera won’t rotate 180 degree to face the front of the car when the player reverses.

Here’s my code, it’s what’s in the FixedUpdate that won’t work:

using UnityEngine;
using System.Collections;

public class CarCameraScript : MonoBehaviour 
{
	public Transform car;
	public float distance				= 8.0f;
	public float height 				= 2.5f;
	public float rotationDampening 		= 3.0f;
	public float heightDampening 		= 2.0f;
	public float zoomRatio 				= 20.0f;
	public float defaultFOV				= 60.0f;

	private Vector3 rotationVector;


	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void LateUpdate () 
	{
		float wantedAngle = rotationVector.y;
		float wantedHeight = car.position.y + height;
		float myAngle = transform.eulerAngles.y;
		float myHeight = transform.position.y;
		Quaternion currentRotation = Quaternion.Euler(0, myAngle, 0);

		myAngle = Mathf.LerpAngle(myAngle, wantedAngle, rotationDampening * Time.deltaTime);
		myHeight = Mathf.Lerp(myHeight, wantedHeight, heightDampening * Time.deltaTime);

		transform.position = car.position;
		transform.position -= currentRotation * Vector3.forward*distance;
		transform.position = new Vector3(transform.position.x, myHeight, transform.position.z);
		transform.LookAt(car);

	}	

	void FixedUpdate()
	{
		Vector3 localVelocity = car.InverseTransformDirection(car.rigidbody.velocity);


		if(localVelocity.z < -0.5f)
		{
			rotationVector.y = car.eulerAngles.y + 180;
		}
		else
		{
			rotationVector.y = car.eulerAngles.y;
		}

		float acceleration = car.rigidbody.velocity.magnitude;
		camera.fieldOfView = defaultFOV + acceleration*zoomRatio*Time.deltaTime;
	}
}

check the localVelocity.z
with print(localVelocity.z)

Is it really < 0.5f a long time?

My bad, it’s supposed to be -0.5 and I did the check and it was, as long as I’m reversing it’ll stay below -0.5.

I just can’t figure this out and I really need it solved today.

Any suggestions anyone? really need a solution.

Thanks :wink:

Quaternion currentRotation = Quaternion.Euler(0, myAngle, 0);

        myAngle = Mathf.LerpAngle(myAngle, wantedAngle, rotationDampening * Time.deltaTime);

You might consider switching those lines.

I can’t thank you enough, you just solved hours of stressing over and it was so simple which I expected it to be.

Thank you so much!