Day 100 — Following the Player Using a Cinemachine Virtual Camera
Hey and welcome!
Unbelievable that it’s been 100 days in a row already, time flies! Let’s celebrate by implementing a good way for the camera to stay on our character so that the player always has a view of what’s going on.
So far we’ve learned that we can just make the Main Camera object a child of the Player object and that will cause the camera to move along with us, however if you’ve tried that in this project you’ll have ended up with something like this:
What’s happening here is that because we’re directly altering the player’s rotation in order to get them to face to the left it also alters the rotation of any object that’s a child of the player. Which in this case includes our camera!
Let’s look into fixing this by first installing Cinemachine by heading to Window > Package Manager and searching for Cinemachine within the Unity Registry.
Once that’s imported a new Cinemachine option should show up in your tool bar at the top, click on that and choose the Create Virtual Camera option to add a new virtual camera into your scene. This should automatically add a cinemachine brain to your main camera so that whatever the virtual camera is looking at that’s what your main camera will look at as well.
If that’s not there then you can click on the add component option to add one to your main camera and it should automatically add the virtual camera to it as well.
In your virtual camera you’ll find a CinemachineVirtualCamera component in there including two properties called Follow and Look At which is looking for the transform of an object. Go ahead and add your player object into both of these and you should see your virtual camera change position to keep the player as the focus.
Adjust your camera so that it’s looking at the player from the side if it’s not doing so already (It will most likely be sitting behind the player!) and then test this out.
Already that feels a lot better and looks a little more cinematic now. I highly recommend playing with the settings here as they can change how the camera will react. As an example you can change the noise property to make the camera feel like it’s being held manually by a human!
You’ll notice though that our camera still rotates with the player when they move to the left so there’s one more thing to do here. If you go to your Follow property and click on the gear icon next to it you’ll see the option to Convert to TargetGroup, click that and it will create a TargetGroup object with a CinemachineTargetGroup script attached to it.
As you might be able to guess the important property for us here is the Rotation Mode, go ahead and change this from Group Average to Manual and watch it do its magic.
Now that’s definitely not looking too shabby at all! With that in place that’s me finished with this project for the time being, I definitely want to come back to this one later on when I’ve learned a bit more because there’s a couple of things I want to tweak with this.
This will also be a good project to play about with when I start my articles about level design later down the line since this only has a few platforms and an elevator in it currently so there’s plenty to improve on.
Here’s to the next 100!