Game Development Reference
In-Depth Information
Figure 5-2. Vector3.up is parallel to the positive y axis
Vectors do have a direction, so while Vector3.up and Vector3.down both parallel the y axis, they
point in opposite directions. Replace Vector3.up with Vector3.down . Save, play and notice that
now the cube appears to be rotating in the opposite direction. This is because the rotation is now
calculated around the downward-directed vector (0, -1 , 0).
You aren't limited to Vector3 shortcuts, and in fact you could pass in a Vector3 variable of any value.
Replace ( Vector3.down ) with the (x, y, z) values of (2, -5, 4), save, and play. Now you see the cube
tumbling at an angle, as the rotation is calculated around this random vector. The cube also appears
to be tumbling faster now that rotation calculations applied to all three axes are added together.
Editing Properties While Playtesting
Now that you can rotate the game object around an axis in any direction you like, it would be nice if
you could control the speed as well. Even better is being able to test and tweak the speed from the
Unity editor, rather than going back and forth to the script.
Back in MonoDevelop, under #pragma strict , declare the public float variable rotationSpeed and
give it a starting value of 5:
public var rotationSpeed:float = 5;
Edit the Update() function to:
transform.Rotate(Vector3.up * rotationSpeed);
You are multiplying the default rotation calculations by a factor you control: the variable
rotationSpeed . Save and return to the Unity editor. With the Cube game object selected in
the Hierarchy, now you will see rotationSpeed appears as a property of the MoveCube script
component, with the initial value of 5 (Figure 5-3 ).
 
Search WWH ::




Custom Search