Working with Unity Timeline

Final Challenge: Making use of Cinemachine & Timeline

Simon Truong
5 min readOct 25, 2022

--

Continuing from the previous article, in this article we will finalize some of the 3D model elements in the spacecraft and also start adding in some of the functions that we desired.

Adding in UI elements:

To start we are going to add in some UI elements that will help direct the player to utilize the functions in the game. I do all my UI elements in vector format and then save them as PNG files. Therefore I will be using Adobe illustrator for this purpose, but other programs are just as useful.

I decided to start with the camera change UI element. I simply want to have an floating image on top of an computer console. Then when the player gets close to that console, another UI element pops up indicating the ability to change cameras, and also the key to press to trigger this function.

Here I have the floating image UI and also the toggle switch UI.

The example below is how I want the UI elements to appear in game.

To make this work, I imported the drawings from the illustrator as PNG files. For the floating image “Change Camera” I decided to use an Sprite Renderer component.

As for the pop up UI element, I need to create an canvas component. Within the canvas I used an UI Image component.

Scripting the UI elements together:

Now it is time to script the toggles together, in this scenario I decided that I will create a new empty game object to place the collider in.

The collider placement that will toggle the UI element, and also the future camera switch.

Inside the collider I will make sure that the “is Trigger” checkbox is turned on. I will also add in a new C# script that will control the function. I named this script “Camera_Toggle”.

In this public class, I am going to basically have two local variables that require the serialized field. The first is the camera UI and the second is the future camera that we will swap to.

To toggle the UI to turn on, I basically used an On Trigger Stay method which will check whenever the player has made contact with the collider. When the player does, the Camera UI (which was set to default to be off) will be turned on.

I have the same type of method but in reverse when the player backs off from the collider.

Before we start scripting the camera swap mechanism we are first going to test out if the UI elements do pop out.

Perfect!

Swapping Cameras:

First we are going to create a brand new Cinemachine camera, I wanted the camera to be able to swivel around the ship so I decided to use the “Free Look Camera”. I place the free look camera around the main ship and made the rings much larger than the ship itself, so when the camera spins around, the player will be able to see all of the ship.

I also change the Camera Priority of the Free look camera to be higher than the regular 3rd person camera. That way when we toggle the free look camera, it will automatically take over from the 3rd person camera.

The 3rd person camera is set by default to be 10.

Next in the Main Camera component within the Cinemachine Brain, we are going to add a custom Blend for the 3rd person camera and Free look camera transition.

Both of these custom blends will only affect these two cameras.

Scripting:

Finally it is time to script, we are going to create a brand new C# script that will control the camera transitions. We are going to start with several local variables, an Game Object which will house the Camera. Two bools, one that will check whenever the player has stayed into the collider, and another bool for when the camera can be swapped.

Next we are going to create the public method that we named back when we create the Collider. This public method is going to toggle one of the bools and also set another bool allowing the ability to swap cameras.

Once you have created this method, the Collider script will be error free.

Finally we are going to actually script the button input and camera trigger. I am using the new Unity Input system, but you are also free to use the older input system (which is much easier)

Once that script is finished, you will be able to swap cameras when the player is near the collider.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.