CarCamera - Disable "look behind" when reversing

I’m using the standard CarCamera script form the Unity Car tutorial. As a whole this does exactly what I want, with the exception of reverse cam.

I want my players to be able to choose to look behind them when reversing by pressing a key - I imagine this is a very simple line in the CarCamera script I’m overlooking, but this has been driving me crazy for the past 5 hours!

I have looked into the AdvancedCameraFollow script, but it doesn’t want to compile for me (scripts copied exactly from the wiki).

Thanks

I have not seen the script you mention, but two possible ways of handling it come to mind:

Attach two cameras, one facing forward (the one you probably have) and another one facing backwards. Only enable one camera at a time, and put them under a single game object. This root game object would be what is “driven” by the car’s position. Finally, write a script to toggle the front camera OFF and the rear camera ON when either a) the player presses a button, or b) the player’s motion is negative (backwards).

ANOTHER way to do it is to put a game object “between” the camera and another object that drives which way the camera faces. Based on the same two above criteria (backing up or a special key), either set the transform.localRotation to one of these two values:

transform.localRotation = Quaternion.identity; // facing forward

transform.localRotation = Quaternion.Euler(0,180,0); // facing back

Something along those lines ought to do the trick for you.

Hi Kurt,

My current script is:

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()
    {
        if(target.root.rigidbody.velocity.magnitude<0.1f)
            return;

        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(55, 72, 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);
       
    }
}

And already covers the reverse view when backing up. I can’t see exactly where this is causing the camera to flip around when driving backwards.

Having another camera activated when key is pressed is exactly what I had in mind, but I need to stop this one form turning around before I do that part.

Thanks

Line 44 choses a new position to place the camera at based on the car’s position, by subtracting the car’s velocity in the direction of motion. That makes the camera get behind the car when the car goes forward, and since it is looking at the car, it looks “pst” the car. Conversely when you back up, it goes the other way.

You probably want to always take the absolute value of the velocity and use that, so the camera is always behind the car, and put in separate logic which reads your “look behind me” button and then flips the camera around to be in front of the car, looking at the car as usual, which will give you a reverse view.

1 Like

HI Kurt,

I just tried absoluting and it produced some very strange results. Having investigated further, it seems velocity.x needs absoluting and velocity.z needs to be negatived. Unfortunatel,y it appears as though the currentVelocity is using world rather than local values as the velocity increases/decreases depending on which direction I am facing!

I will continue to investigate.

Thanks

Anybody got a solution to stop the “reverse” view in this script? I’ve tried smoothfollow but wont allow the “side-on” view while turning :frowning:

Thanks