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).
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:
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.
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.
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!