The Camera

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book.

Modifying the Camera

At this point, we have all the basics in place to run our game. The next thing we are going to want to do is make the camera follow the Player GameObject. To begin, select the Camera GameObject from the Hierarchy panel in the Scene window. We’ll want to start by changing the Background color to black.

We will also want to tweak the Size of the Camera. By default, it was set to a Size of 5. Change that to 2 and you should see the camera is now pulled in closer to the action.

If you run the game, the camera is probably not going to capture the action very well. Let’s look into making a very basic camera follow script. On the Main Camera GameObject, add a new script and call it CameraFollow.

In this script we are going to build a very simple update loop that will focus the camera on a GameObject we set as the target. Add the following two properties to the script:

public GameObject target;
private Transform targetPos;

This will help us store the target GameObject and a reference to its Transform. Now, in the Start method, add the following:

targetPos=target.transform;

Next we will need to tell the camera to update its position to the target’s position. Add this to our Update method:

if (targetPos)
transform.position=new Vector3(targetPos.position.x, targetPos.position.y, transform.position.z);

Now we have an incredibly basic camera follow script so add it to our camera GameObject in the Scene and you should now have a Target property in the panel.

Click on the little circle to the right of the field and find the reference to the Player in the scene.

Now our camera will be focused on the Player during the game. Hit Play and make sure everything is working properly.

As you can see, while this script is basic it gets the job done. Now let’s talk about bringing the rest of the game to life.

Aspect Ratios

The final thing I want to cover is how Unity allows you to preview different aspect ratios. As you know, trying to support all of these different devices is challenging. When you move from platform to platform in the build settings, Unity offers up Aspect Ratio options in the IDE:

At the end of the day, resolution comes down to aspect ratio, is your game 4:3 or 16:9 or even both? Good games can easily support either resolution; here is an illustration from the original game I based this book on:

Here you can see I am supporting two different aspect ratios and three different resolutions. The key is to have a viewport that can expand or contract to adapt to the different resolutions. For an example, at 800×400 pixels you see the minimum screen real estate needed to play the game. On higher resolutions/aspect ratios your field of view is expanded to fill in the extra space. Also, UI should always be “liquid” meaning it can be pinned to different corners of the screen and moves into place based on the current resolution.

Here is what the game is designed to look like at 800 x 480 or 4:3:

You’ll notice that the game elements take up most of the real estate of the screen. Compare this to the 16:9 version:

Here you see a lot more of the action. The UI has expanded out more and you can get a large area of the background as well as what is to the side of the player but everything is still centered nicely.

I have found that mocking out a single game screen with resolution guides on it helps me better visualize how my game will scale across all of the different devices that it will play on. Unity does a great job of helping you figure out what the game is going to look like by taking advantage of the aspect ratio tab in the game tab so make sure you check that from time to time to make sure everything is display as you expect it to.

Subscribe To My Mailing List

Want to learn how to make a game? Not sure where to start? Even if you are a seasoned game maker there is still a lot you can learn from my mailing list. I'll be covering tips and tricks for how to build, release and market games each month.

Simply sign up for my mailing list and also get access to a 50% off discount code for my eBooks and other content. I promise to not spam your inbox!

Join Now