Unity 2020 Mobile Game Development
上QQ阅读APP看书,第一时间看更新

Creating the player

To get started, we'll build a player that will always move forward. Let's start with that now:

  1. Create some ground for our player to walk on. To do that, go to the top menu and select GameObject | 3D Object | Cube.
  2. From there, we'll move over to the Inspector window and change the name of the object to Floor. Then, for the Transform component, set Position to (0, 0, 0) if needed, which we can type in, or we can right-click on the Transform component and then select the Reset Position option.
  3. Then, we will set the Scale values of the object to (7, 0.1, 10):

In Unity, by default, 1 unit of space is representative of 1 meter in real life. This will make the floor longer than it is wide (X and Z), and we have some size on the ground (Y), so the player will collide and land on it because we have a Box Collider component attached to it.

The Box Collider component is added automatically when creating a Cube object and is required in order to have objects collide with it. For more information on the Box Collider component, check out  https://docs.unity3d.com/Manual/class-BoxCollider.html
  1. Next, we will create our player, which will be a sphere. To do this, we will go to GameObject | 3D Object | Sphere.
  2. Rename the sphere to Player and set the Transform component's Position values to (0, 1, -4):

This places the ball slightly above the ground and shifts it back to near the starting point. Note that the camera object (see the camera icon) is pointing toward the ball by default because it is positioned at 01-10.

  1. We want the ball to move, so we will need to tell the physics engine that we want to have this object react to forces, so we will need to add a Rigidbody component. To do so, go to the menu and select Component | Physics | Rigidbody. To see what happens now, let's click on the Play button, which can be seen in the middle of the first toolbar:

As in the preceding screenshot, you should see the ball fall down onto the ground when we play the game.

You can disable/enable having the Game tab take up the entire screen when being played by clicking on the Maximize On Play button at the top, or by right-clicking on the Game tab and then selecting Maximize.
  1. Click on the Play button again to turn the game off and go back to the Scene tab, if it doesn't happen automatically.

We now have the objects for both the floor and player in the game and have told the player to react to physics! Next, we will add interactivity to the player through the use of code.