Car Tutorial - Remove camera flip

Hi all,

I’m playing with the Main Camera in The Car Tutorial, and I want to remove the camera’s 180 degree flip when the car is in reverse. Unfortunately, I can’t seem to find exactly what bit of the code causes this. I keep breaking the camera altogether!

Any help would be appreciated!

Best,
Chowderman

Any ideas?

write here the script please

using UnityEngine;
using System.Collections;

public class CarCamera : MonoBehaviour
{
	public Transform target = null;
	public float height = 1f;
	public float positionDamping = 3f;
	public float velocityDamping = 3f;
	public float distance = 4f;
	public LayerMask ignoreLayers = -1;

	private RaycastHit hit = new RaycastHit();

	private Vector3 prevVelocity = Vector3.zero;
	private LayerMask raycastLayers = -1;
	
	private Vector3 currentVelocity = Vector3.zero;
	
	void Start()
	{
		raycastLayers = ~ignoreLayers;
	}

	void FixedUpdate()
	{
		currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
		currentVelocity.y = 0;
		prevVelocity = currentVelocity;
	}
	
	void LateUpdate()
	{
		float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 70.0f);
		camera.fieldOfView = Mathf.Lerp(40, 90, speedFactor);
		float currentDistance = Mathf.Lerp(7.5f, 6.5f, speedFactor);
		
		currentVelocity = currentVelocity.normalized;
		
		Vector3 newTargetPosition = target.position + Vector3.up * height;
		Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
		newPosition.y = newTargetPosition.y;
		
		Vector3 targetDirection = newPosition - newTargetPosition;
		if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
			newPosition = hit.point;
		
		transform.position = newPosition;
		transform.LookAt(newTargetPosition);
		
	}
}

Try to make the rigidbody velocity always positive, so it doesn’t flip the camera.

Instead of using the CarCamera, use the SmoothFollowCamera

It’s attached to a car which can go in reverse. Forcing the rigidbody’s velocity to always be positive will prevent the car from being able to go in reverse. I just need help identifying which part of the script flips the camera around, and alter it so it won’t do that.

in the script it says that the camer position is giving also by the velocity. So when the velocity is negative, the camera flip. If in your script you add a " - " in the variable that define the vector of the car position it will not flip.
But I recommend you to use the SmoothFollowCamera too. Good Luck

Unfortunately, due to the track, the Smoothfollow script does not work well.

Sheva, I’m trying to find the variable that positions the camera based on the velocity but I can’t find it. I’m very new to coding, so please have mercy :slight_smile:

I’m also trying to make currentVelocity.x absolute (Mathf.Abs) but it isn’t having the correct effect either.

For now use this script. I have to think about the (Mathf.Abs) with Vector, but i’m a bit busy.

/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.

There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform’s position.
*/

// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;

// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu(“Camera-Control/Smooth Follow”)

function LateUpdate () {
// Early out if we don’t have a target
if (!target)
return;

// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;

currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;

// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation
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 -= currentRotation * Vector3.forward * distance;

// Set the height of the camera
transform.position.y = currentHeight;

// Always look at the target
transform.LookAt (target);
}

man, it works. You have to change the currentvelocity.
change this part :
“”“currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime); “””
in this:
“”" currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.rotation * Vector3.forward, velocityDamping * Time.deltaTime); “”"