How to Setup a Panning Camera in Unity Using Cinemachine and Timeline
Objective: To create a panning camera for our sleeping guard cutscene.
We have setup the 2 virtual cameras that now blend between our character Darren as he reaches into the security guards lap to steal the much needed keycard. Let’s take this a step further and incorporate a panning camera for our first virtual camera. We are going to move it slightly to the right and up, also adjusting the angle along the way.
Using the Timeline editor to create Keyframe Animations for small changes to game objects positions, scales or rotations is a fantastic option, as it is very simple to use! Let’s get started….
Create Animation Track in Timeline
The first step is create an Animation Track in Timeline by right clicking on the left hand side → Animation Track. We are then going to drag our ‘Over_The_Shouder_Darren’ virtual camera into the newly created field and it is going to ask us if we want to create an Animator component on our ‘Over_The_Shoulder_Darren’ virtual camera game object? Yes we do:
Creating the Keyframes for the Animation to Move Between
Let’s first get a little more understanding of what we are looking to achieve here using Keyframe Animations. As I am simply looking to move (pan) a virtual camera a small distance to add some depth to our cutscene, what we need to do is create 2 separate keyframes that our virtual camera is going to move between. The first keyframe is going to be it’s starting position and the second is it’s final destination, as Unity interpolates between each keyframe.
Let’s take a look at setting up our first keyframe, by pressing the ‘record’ button on the Animation Track:
You will now notice that your Animation Track is now red with the text ‘Recording’. Any changes that are now made to this game object will be captured and a keyframe will be created on the Timeline Playhead line (white line). Note: If no changes are made, then no keyframe is registered. Let’s dive into this a bit further.
As discussed above, we can’t actually create the starting points keyframe unless we move the game object. So to get this setup, I am simply going to move the virtual camera in any direction and then place it back to where it was (hitting the record button again to end the recording) and then notice the little keyframe symbol pop up on your Timeline where the Timeline Playhead is:
Creating the Second Keyframe
Let’s now move the Timeline Playhead forward on the Timeline (to say 1:50) and create our second keyframe. Hit that ‘Record’ button again, and this time let’s move the camera to the end position and once we are happy with it, press ‘Record’ again.
We now have our second keyframe on the Timeline and we can actually drag the Timeline Playhead and watch the camera pan between 0 and 1:50.
Unity will now interpolate between these 2 positions.
How to Delete a Keyframe
If you have accidentally created the wrong animation or you just simply stuffed up (like I tend to do) and you want to delete the keyframe and start again, you can’t do it directly in Timeline, you will need to right click on the animation track → Edit in Animation window (this will now open the Animation window for you if it wasn’t already open). From here locate the keyframes you want to delete, right click on them → Delete: