Game Development Reference
In-Depth Information
Breaking down this new code:
(1) if(Input.GetKey(KeyCode.N)) {
Check to see if the N key on the keyboard has been pressed. +' Input.GetKey to find it in the
Scripting Reference (Figure 4-15 ). Here you learn GetKey is a boolean value that returns true if the
designated key, in this case N, is pressed. The reference also gives you a handy link to key identifiers
including modifier keys, function keys, and other special keys.
Figure 4-15. Input.GetKey and key identifier link in the Unity Scripting Reference
(2) red = RandomColorValue();
(3) green = RandomColorValue();
(4) blue = RandomColorValue();
(5) alpha = RandomColorValue();
(6) gameObject.renderer.material.color = Color(red, green, blue, alpha);
Identical lines of code from the Start() function for generating a new random material color.
(7) waitTime = 5;
Reset the waitTime to 5. The sphere will still automatically change color to blue five seconds after
the new color change occurs.
Save and play. Now you are using the mouse for camera control, and the keyboard to make changes
to the game object's color—yay!
Cleaning Up Your Code
Look again at the description of lines 2 through 6. They are identical to lines of code from the
Start() function. Scripts with big blocks of duplicate lines of code are “clunky,” meaning less
efficient. When you find yourself using identical lines of code, this is an indication that it's time to
streamline your code by creating a single function containing the duplicated block of code, then
using a simple function call in place of the code blocks.
 
Search WWH ::




Custom Search