Unity 2018 Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How it works...

You have created a player character in the scene using the girl1 sprite, and added a scripted component instance of class PlayerMove. You have also created a star GameObject (a pickup), tagged Star and with a 2D box collider that will trigger a collision when the player's character hits it. When you run the game, the player-girl1 character should move around using W A S D, the arrow keys, or joystick. There is a newVelocity variable, which is updated each frame in the Update() method based on the inputs. This Vector2 value is then applied in the FixedUpdate() method to become the new velocity for the GameObject. 

Unity maps user inputs such as key presses, arrow keys, and game controller controls to its Input class. Two special properties of the Input class are the Horizontal and Vertical axes – accessed via the Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") methods.

Managing your input mapping: You can manage the mapping from different user input methods (keys, mouse, controllers, and so on) to the axes through menu:  Edit | Project Settings | Input

Currently, nothing will happen if the player-SpaceGirl character hits a star because that has yet to be scripted.

You have added a background (GameObject background-blue) to the scene, which will be behind everything since it is in the rearmost sorting layer, Background. Items you want to appear in front of the background (the player character and the star, so far) are placed on the Foreground sorting layer.

We can learn more about Unity tags and layers at  http://docs.unity3d.com/Manual/class-TagManager.html .