Quantcast
Channel: Answers for "Javascript question."
Viewing all articles
Browse latest Browse all 4

Answer by aldonaletto

$
0
0
A function *does* something, while a variable *stores* something (a function may also return a value, but that's another story - don't worry about this now). Variables usually have a type: **int** holds only integer values (1, 5, 432), **float** holds real values (1.0, 0.2, 3.14159), **String** holds characters and strings ("a", "hello", "Bye"), and the Unity types can hold references to Unity components or objects (like Transform, Rigidbody, Collider, Camera etc.)

There's an example of a function and a variable:
var speed: float = 60.0;
function Update(){
    transform.Rotate(0, speed * Time.deltaTime, 0);
}
The variable **speed** declared at the top stores a float number - the rotation speed, in this case.
The function Update is called by Unity before drawing each frame. We usually place inside it code that updates the screen or that must execute frequently. In this case, I called the transform function Rotate, which rotates the object around the axes x, y and z by the corresponding values in degrees: x = 0, y = speed * Time.deltaTime, z = 0, in this case. Notice that the object should rotate around the Y axis, and at **speed** degrees per second. Since Update is executed before every frame, Unity supply a special variable to help us: Time.deltaTime is the time elapsed since the last frame was rendered, and works as a "correction factor": when multiplied by **speed** it ensures the rotation will be constant and equal to **speed** rotations per second.

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images