4. Player Input

uke 的头像

For our first program we’re going to allow the user to move around in a simple game world.
Setting the scene
- Start Unity.
Firstly, let’s create a surface for the user to walk on.
The surface we’re going to use is a flattened cube
shape.
- Create a cube and scale its x,y,z dimensions to 5, 0.1, 5 respectively, it should now
resemble a large flat plane. Rename this object ‘Plane’ in the Hierarchy View.
- Create a 2nd cube and place it at the centre of this plane. If you can’t see the objects in
your Game View, alter the main camera so they’re visible. Rename the object to
Cube1.
- You should also create a point light and place it above the cubes so that they’re more
easily visible.
- Save the scene by selecting File->Save As and give the game a name.
Our first script
We’re now ready to start game programming. We’re going to allow the player to move
around the game world by controlling the position of the main camera. To do this we’re
going to write a script which will read input from the keyboard, then we attach (associate)
the script with the main camera (more on that in the next section).
- Begin by creating an empty script. Select Assets->Create->Javascript and rename this
script to Move1 in the Project Panel.
- Double-click on the Move1 script and it will open with the Update() function already
inserted (this is default behaviour), we’re going to insert our code inside this function.
Any code you insert inside the Update() function will be executed every frame.
2
In order to move a game object in Unity we need to alter the position property of its
transform, the Translate function belonging to the transform will let us do this. The
Translate function takes 3 parameters, x, y and z movement. As we want to control the
main camera game object with the cursor keys, we simply attach code to determine if the
cursor keys are being pressed for the respective parameters:
function Update () {
transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
}
The Input.GetAxis() function returns a value between -1 and 1, e.g. on the horizontal axis,
the left cursor key maps to -1, the right cursor key maps to 1.
Notice the 0 parameter for the y-axis as we’re not interested in moving the camera
upwards. The Horizontal and Vertical axis are pre-defined in the Input Settings, the names
and keys mapped to them can be easily changed in Edit->Project Settings->Input.
- Open the Move1 Javascript and type in the above code, pay close attention to capital
letters.
Attaching the script
Now that our first script is written, how do we tell Unity which game object should have this
behaviour? All we have to do is to attach the script to the game object which we want to
exhibit this behaviour.
- To do this, first click on the game object that you wish to have the behaviour as defined
in the script. In our case, this is the Main Camera, and you can select it from either the
Hierarchy View or the Scene View.
- Next select Components->Scripts->Move1 from the main menu. This attaches the
script to the camera, you should notice that the Move1 component now appears in the
Inspector View for the main camera.
Tip: You can also assign a script to an game object by dragging the script from the
Project View onto the object in the Scene View.
- Run the game (press the play icon at the lower left hand corner), you should be able to
move the main camera with the cursor keys or W,S,A,D.
You probably noticed that the camera moved a little too fast, let’s look at a better way to
control the camera speed.
Delta time
As the previous code was inside the Update() function, the camera was moving at a
velocity measured in meters per frame. It is better however to ensure that your game
objects move at the more predictable rate of meters per second. To achieve this we
multiply the value returned from the Input.GetAxis() function by Time.deltaTime and also
by the velocity we want to move per second:
3
var speed = 5.0;
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
- Update the Move1 script with the above code.
Notice here that the variable speed is declared outside of the function Update(), this is
called an exposed variable, as this variable will appear in the Inspector View for whatever
game object the script is attached to (the variable gets exposed to the Unity GUI).
Exposing variables are useful when the value needs to be tweaked to get the desired
effect, this is much easier than changing code.