Game Development Reference
In-Depth Information
Figure 5-3. The rotationSpeed public variable appears as the Rotation Speed property in the Inspector
You changed the Vector3 parameter back to Vector3.up just to make it easier to eyeball changes
you make as you playtest and make adjustments to the rotationSpeed property.
Play, and see the cube rotate about the y axis again. While still in Play mode, change the
rotationSpeed value (note the Cube must be selected). Try faster, slower, zero, and even negative
values. This is a powerful feature of Unity where you can instantly see the effect of your changes as
you fine-tune the game experience. Exit Play mode, and notice that the rotationSpeed value reverts
to 5. Remember, changes made while in Play mode aren't saved.
In Chapter 4 you learned about frame rates and Time.deltaTime . As you make adjustments to
the game object movement, remember that the game objects will move differently on different
computers, based on the frame rates. The solution was to multiply by Time.deltaTime to make the
motion frame-rate independent. Edit the Update() function to the following:
function Update () {
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
Add User Interaction
Save and play. The speed of the cube has changed with this additional factor, but you can readjust
your rotationSpeed property to get back to the speed that you like. The difference now is that anyone
who plays your game on a different machine will have the same experience you create on yours.
Add some user interaction via the keyboard much like you did in Chapter 4, this time for controlling
the cube rotation. Edit the Update() function by adding a conditional that checks to see if the user is
pressing the left arrow key.
function Update () {
if(Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
}
Save and play. Now the cube rotates only when the left arrow is pressed. Similarly you can let the
user rotate the cube in the opposite direction with the right arrow key, by using -rotationSpeed .
function Update () {
if(Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
 
Search WWH ::




Custom Search