Creating A look back mechanic in Unity

Curly fries
7 min readOct 30, 2021

Welcome all, recently I implemented a look back camera to my game to enhance player experience.

Now, I’m going to discuss how I implemented the feature and why? The game is built using the Unity game engine and this mechanic makes use of the cinemachine package. Perhaps, fellow unity developers may find this post interesting.

Why?

Firstly, let’s discuss the “why” I implemented this feature; if you’re only interested in seeing the “how”, then feel free to skip this section and scroll down.

Anyway, let me provide some context, I am developing a comedy survival horror game titled ARGHH, there’s a killer chasing me! (Ridiculous name, I know.) Evidently, by the name, the enemy chasing you is a big part of the game.

Sometimes in various stealth or survival horror games, when the enemy spots you from behind, you have no idea how far or close they are. Additionally, it’s a real hassle to quickly rotate the camera behind you to see them. Anddd you’d have to do that whilst holding the controls to run away from them or you’ll get caught.

If that’s not already a pain, don’t forget that most controls are based on the camera’s position. As soon as you rotate the camera behind you, you’ll have to quickly adjust your controls to run towards the camera’s new position.

Doing all that sounds like a pain, and it’d be an even bigger frustration in my game due to the stats of the main character.

Adam, my game’s protagonist, has below average speed. Thus, the chances of him outrunning the killer…is slim. Though, it is possible if the killer is quite a distance from him.

But for the player to determine whether they should run and try to shake the killer off or immediately switch to combat as the killer is too close, a gauge of the killer’s distance from them is necessary.

I suppose the player could simply rotate the camera as I suggested above, but that would mean crucial time is wasted trying to find the killer and at that point running is redundant because the killer’s knife is probably already inside Adam’s ribs. Ouch!

The solution I designed for this problem is…the look back camera! Ding, ding, ding, you guessed it!

Let’s move on to the implementation of this feature.

Implementing the Look Back Camera

So, here’s how the look back camera works: when the player holds down a button, the look back camera is activated. Whilst activated, the look back camera’s position shouldn’t affect movement controls. The movement controls should still be driven by the main third person camera. (meaning the player should still move in the direction of the third person cam despite not being the active camera). Once the button for the look back camera is released, it’s deactivated and the third person cam is active.

Alright, let’s get started. I already have my third person camera and player controller setup. The player controller’s movement is influenced by the third person cam. So, the player moves in the forward direction of the camera when hitting the W or Up arrow Key.

The game’s main camera has a cinemachine brain component. Basically, other cinemachine virtual cameras in the scene tell the camera with the cinemachine brain what to do.

Cinemachine virtual cameras aren’t actually cameras (hence, the “virtual”). Rather they hold data about a camera shot (data like camera position, rotation, target to follow, target to look at, field of view, etc) which is passed onto the actual camera with the cinemachine brain which directs the camera based on the received virtual camera data.

My third person camera setup consists of a free look Vcam that drives my main camera. The free look cam is a type of Vcam that orbits around the player based on user input providing a third-person camera experience.

So, how do I create a look back camera without affecting the controller movement? Hmmm…I could just add another virtual camera to the scene and place it in a position that shows what’s behind the player. Once activated, the main camera with the cinemachine brain will now be directed by this look back Vcam (V cam is shorthand for virtual camera)

Yeah! That could work, right?

Uhhh, unfortunately, no. This method deactivates the free look cam which means the now active look back Vcam would affect the player’s controls. That’s not the desired behaviour.

The cinemachine brain can only receive data from one camera at a time. It decides which camera should direct it based on which VCam is active, and if multiple are active it will select the Vcam with the higher priority.

Each Vcam has a priority field, the higher the priority the…you get the picture.

So, what I require is a solution where the free look cam is still active but the main camera with the cinemachine brain ignores the look back Vcam even when active.

Therefore, I need a new camera with its own cinemachine brain.

I add a new camera to the scene and call it Look Back Cam and give it its own cinemachine brain. However, this look back cam should ignore the free look Vcam.

To do this I put the free look Vcam on a layer called Cam.

I then edit the Look back cam’s culling mask and remove the cam layer. The culling mask determines what part of the scene is rendered. Therefore, removing a layer will tell the camera to not render gameObjects on that layer, rather just simply ignore them.

That’s what the look back cam does, it simply ignores the free look Vcam even when it’s activated.

Now I need to create a Vcam for the look back cam.

I create a new Vcam; name it Look Back Vcam; position and rotate it how I want. Then put it on a layer called Look Back Cam.

I add Look Back Cam layer to the Look Back Cam’s culling mask and I remove the Look Back Cam layer from the main cam’s culling mask.

Now that’s done, all we need to do is activate the Look Back Cam when the player presses a button. I have a script called Camera Manager and I am using the new unity input system.

The Camera Manager needs to know about the Look Back Cam. So, I create a serializefield for the Look Back Cam and drag it in the inspector.

The Look Back Vcam can remain active at all times because the main camera ignores it. But we need to only activate the Look Back Cam when the player holds down the button.

Therefore, the script is easy:

On button press: activate Look Back Cam. On Button release: deactivate look back cam.

(I have added a clearer version of the code and a shorthand version. The function is called from my player Controller script and passes in a argument of true if Look Back button was pressed and false when button is released)

Notice how the freelook Vcam is untouched. It’s unseen by the player but remains active. Therefore, the movement controls can still be influenced by it.

The main camera, also, remains active and that’s fine because I’m going to do this:

I give the Look Back Cam a higher priority than the main camera. Therefore, whenever it is activated, it’s the camera the player sees. Upon deactivation, the player sees through the main camera!

And that’s it!

Lemme know in the comments what you thought of the tutorial. Was it explained well? Was it helpful? Etc. I’d highly appreciate your feedback as it’d allow me write better tutorials in the future.

If you’re interested in the development of my game or would like notifications on new tutorials, be sure to follow me on social media:

My Game | Twitter | Instagram |Linktree

--

--

Curly fries
0 Followers

A self-taught Game Dev who writes their own stories. https://linktr.ee/curlyfries709