Game Development Reference
In-Depth Information
Listing 1.17 A Script to Make a Game Object Fall Down
to a Certain Height and Then Grow
var speed: float = 0.1;
var growthRate: float = 0.01;
var groundLevel: int = -4;
function Update ()
{
if(transform.position.y > groundLevel)
{
//keep moving down
transform.position.y = transform.position.y - speed;
}
else
{
transform.localScale.x =
transform.localScale.x + growthRate;
}
}
Step 6. Save and play. When the sphere reaches a y position of 4 it
will stop moving and start to grow along its x axis.
The other programming construct that handles condition statements is
a loop . A loop is a segment of code that runs over and over again until an
ending condition is met. There are several types of loop constructs, but for
now we are going to have a look at just one, the for loop .
Consider the code in Listing 1.18 .
Listing 1.18 JavaScript for Printing Numbers between 1 and 5
var i = 1;
print(i);
i = i + 1;
print(i);
i = i + 1;
print(i);
i = i + 1;
print(i);
i = i + 1;
print(i);
i = i + 1;
Search Nedrilad ::




Custom Search