Conditional statements controlling multiple properties
We created 3 spheres for this project, didn't we? Let's bring Ball_3 into play and have it also controlled by the conditional statement.
1. Change the previous expression so it reads as follows:
if( Ball_2.Position.Y <= 4 )
{
Ball_1.Position.Y = abs( Ball_2.Position.Y - 4 );
Ball_3.Position.Y = abs( Ball_2.Position.Y - 4 );
}
else
{
Ball_1.Position.Y = 0;
Ball_3.Position.Y = 0;
}
Now when you move Ball_2, both Ball_1 and Ball_3 will follow as Ball_1 originally did. Because we need to control which statements lie within the if part of the argument and which lie within the else part, we are required to add braces to hold the statements together. Within each set of braces, we could add as many statements to execute as we'd like; the key is that each statement must end with a semicolon and be contained within the braces.
Chains of conditional statements
Conditional statements can be extended to contain multiple if components; to do this, we chain together conditional statements in the form of if, else if.
We can do just that with the previous example by limiting how far up Ball_1 and Ball_3 will travel. Currently the outside spheres start at 0 and move upward as the center sphere moves down; however, there is no limit currently to the upward travel of Ball_1 and Ball_3. We will build in that limit:
1. Open the previous expression and lets do some reorganizing of it as follows:
if( Ball_2.Position.Y >= 4 )
{
Ball_1.Position.Y = 0;
Ball_3.Position.Y = 0;
}
else if( Ball_2.Position.Y <= 0 )
{
Ball_1.Position.Y = 4;
Ball_3.Position.Y = 4;
}
else
{
Ball_1.Position.Y = abs( Ball_2.Position.Y - 4 );
Ball_3.Position.Y = abs( Ball_2.Position.Y - 4 );
}
Through this reorganization of the previous script, we've called out the extents of how far Ball_1 and Ball_3 should travel.
The first if statement asks if Ball_2 is at 4 or higher, then Ball_1 and Ball_3 stay at 0 (the lowest limit).
The next part of the expression then asks if Ball_2 is at 0 or lower, and if so, Ball_1 and Ball_3 are set to 4.
If neither of these questions are true, then the else statement gets executed, effectively placing Ball_1 and Ball_3 somewhere between the two limits.
As with adding multiple properties to be controlled by a conditional statement, you can add as many else if chains to your conditional statement as long as the proper structure is followed
Summary
In this section, you learned how to:
- create a conditional statement that controls aan animtion channel based on the state or condition of another animation channel or variable
- control multiple animation channels through a single conditional statement
- set up a chain of conditional statements to refine the amount of control you have over a specific or multiple animation channels.
|