Time for action – creating a following third-person camera
A large portion of games use some variant of a following third-person camera, including marble-rolling games like the one we've been using as an example. These cameras typically follow the target around while maintaining a set distance and elevation.
First, we'll need to disable our current camera code, and then we can create a new script for our new camera code. Perform the following steps to create the camera:
Click on the Main Camera object in the Hierarchy window to display its properties in the Inspector window.
Uncheck the box next to the Tracking Camera component to disable it.
Disabled components are still present on objects, but they don't function as long as the checkbox is empty as shown in the following screenshot:
Create a new script called
ThirdPersonCamera.cs
in yourCode
folder, attach it to the Main Camera object in the Hierarchy window, and open it in your code editor.The first thing we'll add to the camera is a function...