官术网_书友最值得收藏!

  • Unity 2020 By Example
  • Robert Wells
  • 532字
  • 2021-06-11 17:57:21

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:

  1. 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

  2. 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:

  1. Select the camera in the scene.
  2. 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.

  3. 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.

主站蜘蛛池模板: 色达县| 青铜峡市| 石楼县| 霍州市| 久治县| 民县| 安国市| 喜德县| 桐乡市| 娄底市| 泽州县| 闽清县| 织金县| 读书| 南昌市| 泾阳县| 城步| 义乌市| 项城市| 萨迦县| 石家庄市| 枣阳市| 县级市| 金塔县| 吴桥县| 大洼县| 镇坪县| 富锦市| 锦州市| 凌源市| 民和| 平阳县| 如东县| 海阳市| 文成县| 涿州市| 德惠市| 富锦市| 泾阳县| 临沂市| 深州市|