Every Unity game features a camera. This camera can represent the player’s perspective, but you can use cameras for a whole variety of purposes. Cameras can capture cut scenes. They can project their contents on external models such as an in-game monitor. You can use them to show other characters when they speak to the player.

In your game, you are going to put the camera behind the space marine, so the player can see all the action. The camera will track the player. This does pose a problem. The player may not be able to see any of the aliens behind the camera. This will actually provide a little bit of tension as the bugs spawn all around the space marine. There is a danger of setting the camera too close or far.

The big takeaway is that the camera can make or break your game. A single bad camera placement can ruin your game in frustration. You should be judicious where you decide to place it.

Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.

Understanding camera projection

Unity’s camera has the concept of a projection. This determines how your games will appear to the player. This works hand in hand with the type of game you are making.

There are two different projection types: perspective and orthographic.

The perspective projection works just like your eye. You see things in 3D. Objects close to you appear large, whereas objects in the distance will appear smaller. This project makes sense when working with 1st person shooters or 3rd person exploration games.

Orthographic projection ignores depth. Objects far away will appear the same size as an object up close. Typically, you use this kind of projection for 2D games, but you can also use it for 2.5D games as well.

This is an illustration. On the left, two cubes are arranged. One looks closer to the camera, whereas the cubes on the right look combined.

This image illustrates the two types of projections. The cubes on the left are viewed using a perspective projection, whereas the cubes on the right are using orthographic.

That said, cameras have plenty of options in Unity. Do yourself a favor and review all the different properties in the Inspector. Remember, mousing-over the property name will provide a detailed explanation.

Positioning the camera

As is, the camera is in a somewhat poor position. It’s seeing through the floor of the arena. Unfortunately, this style of game isn’t quite mainstream … yet. For now, you’ll need to adjust the camera.

One cool feature of Unity is that it provides a preview camera in your Scene view. Near the body of the Scene view, click the second to the last button on the toolbar. Your preview will appear.

This shows the Scene view with the camera preview in the lower right.

It’s pretty useful when you have the Game view hidden and need to do some position.

For this game, you must position above the player and rotate it down. Remember, the camera is just a regular component. It attaches to a GameObject. When you position and rotate the GameObject, you are adjusting the camera.

In the Hierarchy, select the Main Camera, and in the Inspector, set Position to (-9.7, 53.6, -56.1) and Rotation to (30, 0, 0). I determined these values by moving the camera around manually and looking at the Camera preview in the lower right until I was happy with the result.

In the Camera component, set Field of View to 31. This effectively “zooms in” the view a bit.

This shows the configured Main Camera.

Now play your game. In the Game view, you’ll be able to see the action, although the camera doesn’t follow the player. Before you customize the camera, you understand Unity’s Game view.

Understanding Unity’s Game view

The Game view is a key view inside of Unity. This is where you can actually play your game. The Game view is where you actually play the game.

You’ve already used the controls to play your game.

This shows player controls on the Game view.

Besides the play button, there are two others: a pause button and a pause stepper. Pause allows you to, well, pause your game in motion. The stepper allows you to step through the animation one frame at a time and is especially useful for debugging animation issues.

Unfortunately, there are two life-and-death details to keep in mind as you play.

First, when you start playing, the interface becomes darker to give a visual cue that you’re in play mode.

Second, when you play your game, you can change anything about it (including changing values on components in the inspector) but, when you stop playing the game, ALL YOUR CHANGES WILL BE LOST.

Live editing is a blessing and a curse. The blessing is that you can tweak the game without consequence. You can add enemies, change the world geometry, and really, make any change to your game as it is running. The curse is that sometimes you forget you’re in play mode, continue working on your game then, for some reason, the game stops. Poof! Buh-bye changes!

Thankfully, you can (and should) make play mode really obvious. Select Edit\Preferences on PC or Unity\Preferences on Mac to bring up a list of options.

Select the Colors section. Look for Playmode tint. Click the color box next to it, and then give it an unmistakable color — I prefer red. Now play your game to see if it’s conspicuous enough.

There are ways to save your changes in play mode. There’s even an extension that will do it for you. Just keep in mind, losing changes to play mode is a rite of passage for any Unity developer.

Prepping the camera

Now comes the time to move the camera. Since the camera is just another GameObject, moving the camera is as easy as changing the transform position.

Instead of manipulating the camera directly, you’ll manipulate a parent GameObject that will be your camera mount. That gives you a lot of flexibility moving it around the 3D space. The basic idea is you want this camera mount to represent the position the camera should focus on and have the camera be relative to this position.

In the Hierarchy, click the Create button and select Create Empty. Name it CameraMount.

Initially, you want the camera to focus where the space marine is, so let’s configure the CameraMount to be at the same position as the space marine.

To achieve this, select the space marine, click on the three dots to the upper-right of the Transform component. Select the Copy category, and then select the Position option.

This shows copying the Position values from the Space Marine.

Then select the CameraMount, click on the three dots to the upper right of the Transform component, and select Paste and from the options, select the Position option.

This shows pasting the position to the CameraMount.

Next, drag the Main Camera GameObject into the CameraMount GameObject.

This shows moving the MainCamera to the CameraMount in the hierarchy

Now to write the movement code. For this, you’ll need to write some C# code.

Writing the camera movement code

At this point, it’s time to create another script. Unity provides many ways to create scripts. There is no “right” way. Throughout the series, you’ll be presented with different ways just to illustrate the various options available.

With the CameraMount selected, click the Add Component button in the Inspector and select New Script. Call it CameraMovement and then click Create and Add.

When you make a new script by clicking the Add Component button, Unity will create the script in the top level of your assets folder. Get into the habit of moving assets into their respective folders the moment you make or see them.

Drag your new file from the top level of the Assets folder into the Scripts folder.

Double-click the CameraMovement script to open it in your code editor. Underneath the class definition, add the following variables:

[SerializeField]
private GameObject followTarget;

[SerializeField]
private float moveSpeed;

followTarget is what you want the camera to follow and moveSpeed is the speed at which it should move. By creating these as public variables, Unity will allow you to set these within the Unity editor itself, so you can set the followTarget to the space marine and fiddle with the moveSpeed to your heart’s content, as you’ll see shortly.

Now add the following to Update():

if (followTarget != null) 
{
  transform.position = Vector3.Lerp(transform.position, 
    followTarget.transform.position, 
    Time.deltaTime * moveSpeed);
}

I added breakpoints in the code to make it easier to read and understand, but in your editor, it should look like the following:

This shows the code in the editor. The code is written on one line as opposed to having line breaks.

This is true for all code segments in this series.

This code checks to see if there is a target available. If not, the camera doesn’t follow.

Next, Vector3.Lerp() is called to calculate the required position of the CameraMount.

Lerp() takes three parameters: A start position in 3D space, an end position in 3D space, and a value between 0 and 1 that represents a point between the starting and ending positions. Lerp() returns a point in 3D space between the start and end positions that’s determined by the last value.

For example, if the last value is set to 0 then Lerp() will return the start position. If the last value is 1, it returns the end position. If the last value is 0.5, then it returns a point halfway between the start and end positions.

In this case, you will supply the camera mount position as the start and the player position as the end. Finally, you multiply the time since the last frame rate by a speed multiplier to get a suitable value for the last parameter. Effectively, this makes the camera mount position smoothly move to where the player is over time.

Save your code and return to Unity. Select CameraMount in the Hierarchy, and look in Inspector. You’ll see two new fields named Follow Target and Move Speed. As mentioned earlier, these were automatically derived by Unity from the public variables you just added to the script. These variables need some values.

With the CameraMount still selected in the Hierarchy, drag SpaceMarine to the Follow Target field and set the Move Speed to 20.

The bigger the move speed, the faster the camera mount will move to the player. The smaller, the more the camera will “lag” behind the space marine’s position, letting the player “jump ahead” of the camera. Try changing the move speed to a smaller value, like 2, and see what happens for yourself!

Play your game.

This shows the space marine clipping through a column.

The space marine moves and the camera follows him. Granted, he can’t turn, and he walks right through objects just like Kitty Pryde, but these are easily solvable issues that you’ll tackle in the next chapter, Chapter 4, “Physics.”

Where to go from here

Unity provides a ton of options when working with cameras. Remember, you can have multiple cameras in a scene, and you can use cameras to project their contents onto the game world. You can have a lot of fun with them.

Just remember, the main gameplay camera can either make or break a game. Be diligent how to use it and make sure to listen to feedback from your players. Ideally, you’ll do lots of testing before launching your game.

Coming up, you’ll add the ability to shoot the marine’s gun. You will need to create lots of GameObjects in your code. You’ll do this in the next tutorial.


Discover more from Jezner Blog

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply