Game Development Reference
In-Depth Information
Listing 1.13 A Script to Move a Game Object in a Circle
var radius: float = 5;
function Update ()
{
transform.position.x =
radius * Mathf.Sin(Time.fixedTime);
transform.position.y =
radius * Mathf.Cos(Time.fixedTime);
}
Step 6. This new JavaScript file can be added to the Sphere while
the grow script is still attached. Select revolve in the Project and drag
and drop it onto the Sphere in the Hierarchy. Select Sphere in the
Hierarchy and check the Inspector to see that revolve is attached. If it
is not attached, try the drag and drop process again.
Step 7. Play the application. The Sphere will resize slowly while
moving in a circular motion.
Note
The parametric form for a circle is used in Listing 1.13 to move a
sphere around the circumference of a circle. These equations are
based on modifying the x and y positions with cosine and sine
functions and the time. The value of Time.fixedTime is a built-in
Unity variable holding the time in seconds since the application
started running. It is therefore a dynamic and constantly changing
value. Unity also contains functions for all mathematical operations,
such as Mathf.Sin() and Mathf.Cos() .
It is not expected at this early stage in learning Unity to know this type of
information. However, it can be handy to know where to find it, and the most
invaluable resource you should have at your disposal is the Unity Scripting
Reference . To access this information, while in Unity, select Help > Script
Reference from the main menu. In the Web site that opens, try searching
for Mathf and Time to see what is available to you.
1.5.6 Conditional Statements
Conditional statements allow the flow of a program to change when certain
conditions are met (or not met). They rely on Boolean algebra for making
decisions on which way code should flow. Conditional statements can divert
code to other parts of code or can make the same statement of code repeat
over and over.
Search Nedrilad ::




Custom Search