In previous parts we've created an avatar that will always run forward non-stop, and we've also created a track that will automatically add sections as needed. However, we are still lacking a very important element of our game: a camera.
In our case, we are making an infinite runner where your avatar is running in the Z direction, so that means the camera will always follow the avatar from behind. This is important because it defines how the camera should be configured.
Making a basic camera that follows your avatar is pretty simple. First, change your avatar's tag to "Player" in the Inspector tab. Tags make objects more manageable, and can be used to help your functions find desired objects.
Then, pick your camera object in your scene (when you create a new empty scene in Unity, a camera is automatically created, so it should be there. If your scene has no camera, then create one), and add an FSM component to it, and then create the following state machine.
This is what this state machine does: first, it looks for your avatar (notice I've defined a name, and also a tag in the "find game object" action), and assigns that avatar as a variable. And then, it sets that variable as target object of the "smooth follow" action.
If you press Play, you should now have a camera that follows your avatar. However, the default configuration is not very game friendly (the avatar being right in the middle, at a distance not ideal at all), so it's time to tweak it a little.
You don't need to stop the game and change the settings in the "smooth follow" action to do it (depending on your PlayMaker preferences you may need to change a couple of things to enable in-game editing, though). Now, click the edit button in your camera's PlayMaker component and start tweaking your settings until you reach something you like.
Once you find something you like, click on your FSM component little gear in the inspector and click "copy," and when you stop the game, click the little gear again and select "paste values." This way, you will keep the values that would otherwise be lost when you press stop.
One thing you may notice is that, regardless of how much you tweak it, your avatar will always remain right in the middle of the screen. However, you can use an extra object to direct the camera wherever you want, and "offset" your avatar downwards.
The first thing you need to do is create an object (let's use a cube) as a child of your avatar, and move it up slightly. Name your cube "camTarget." Then, you will need to add another action to your camera's state machine so the game looks for that child object and uses it as a camera target. The following image shows how it should look like.
If you press play again, you will notice the camera is now looking at the cube, not your avatar. Also, since the cube is above the avatar, you have successfully displaced the character downwards, making it easier to see what's ahead. Right now all you have to do is tweak the height of the cube and the settings of your "smooth follow" action to get the result you want.