- Unity 2020 By Example
- Robert Wells
- 532字
- 2025-02-28 16:26:48
Configuring the cameras
In this section, we'll configure the scene and game camera. Remember that any changes to the scene camera won't change the playable game. It is only used to navigate the scene to help you create it, whereas changing the game's camera will change how the player views the game.
We'll start with the scene camera:
- Switch the Scene viewport to a top-down 2D view by clicking on the up arrow in the top-right corner of the Scene viewport:
Figure 3.13 – Changing the viewport perspective
You can confirm that the viewport is in a top view because it will list Top as the current view:
Figure 3.14 – Top view in the Scene viewport
- From here, you can have the scene camera conform to the viewport camera exactly, giving you an instant top-down view for your game. Select the camera in the scene (or from the Hierarchy panel and choose GameObject | Align With View from the application menu).
This makes your game look much better than before, but there's still a problem. When the game is running, the spaceship still doesn't look at the mouse cursor as intended. This is because the camera is a Perspective camera, and the conversion between a screen point and world point is leading to unexpected results. We can fix this by changing the camera to an Orthographic (2D) camera:
- Select the camera in the scene.
- In the Inspector, change the Projection setting from Perspective to Orthographic:
Figure 3.15 – Changing the camera to orthographic mode
Every orthographic camera has a Size field in the Inspector, which is not present for perspective cameras. This field controls how many units in the world view correspond to pixels on the screen. We want a 1:1 ratio between world units to pixels to ensure that our textures appear at the correct size, and that any cursor movement has the intended effect. The target resolution for our game will be Full HD, which is 1920 x 1080, and this has an aspect ratio of 16:9. For this resolution, set the orthographic size to 5.4, as shown in Figure 3.15.
Important note
The formula used to calculate the orthographic is screen height (in pixels) / 2 / 100 (Pixels per Unit). Don't worry if this isn't very clear at this stage; it is covered in detail in Chapter 5, Creating a 2D Adventure Game. For now, it is enough to know that our assets use a pixel per unit value of 100, hence 1080 / 2 / 100 = 5.4.
- Finally, make sure that your Game tab view is configured to display the game at a 16:9 aspect ratio. If it isn't, click on the aspect drop-down list at the top-left corner of the Game view and choose the 16:9 option:

Figure 3.16 – Displaying the game at a 16:9 aspect ratio
Now try running the game, and you have a player spaceship that moves based on WASD input and also turns to face the mouse cursor. Great work! The game is taking shape. However, there›s lots more work to do, including creating our first reusable component: the Health component.