Game Development Reference
In-Depth Information
Here you find that Mathf.Round is a static function that accepts a float data type as an argument,
followed by some examples. Go ahead and copy-paste some of these examples to MonoDevelop
and give them a try.
Notice the last comment in the description tells you that the Mathf.Round function doesn't round
everything that ends in .5 up, but rather up or down, whichever way yields an even number. In the
fourth example given, you'll see that 10.5 is rounded down to 10 rather than up to 11 like you would
expect. When you look anything up in the Scripting Reference, take the time to read the entire
description—it may save you some aggravation later over behavior that Unity tried to tell you about.
Great job! You've made it through the basics of variables and functions. You are getting comfortable
with referring to the Unity Scripting Reference and using the built-in functions that Unity provides.
Don't worry if the concepts seem a little fuzzy; you are going to get a lot of practice from here on out.
Keep working the examples and with practice you'll soon get the hang of it.
Conditionals
In addition to mathematical calculations, your scripts will also use logic functions to define the rules
of your game, such as how many hits before an enemy is destroyed, a character's behavior based
on accumulated power and abilities, and of course the conditions that must be met for the big win.
Boolean Logic and Conditionals
A boolean type variable only has two possible values: true and false .
In MonoDevelop, edit your script to the following:
var skyIsClear : boolean = true;
function Start () {
Debug.Log(skyIsClear);
}
Run your script and the console displays true . Boolean variables are often used to monitor or “flag”
a state or condition. For example, I might say, “If the sky is clear then I will go out and play.” I need
to test to see if the sun is up, then make a decision based on that condition.
Update the code in the Start() function to:
if (skyIsClear == true) {
Debug.Log("I will go out and play");
}
This code is fairly readable. The condition you wish to test goes in parentheses after the keyword
if . If the condition is met, then the statement block within the braces is executed. Notice that the
comparison operator for “is equal to” is == , as opposed to the assignment operator = that you use
for setting the values of variables. Go ahead and run the script. Yay—I get to go outside and play!
 
Search WWH ::




Custom Search