Game Development Reference
In-Depth Information
As faster processors are manufactured for new game consoles and devices, older devices will have
relatively slower clock speeds. At some point the older chips just can't keep up with the complexity
of newer games and game engines. The more efficient your code, the easier it is for older chips to
handle your game, in turn making your game available to a broader market.
Using the most efficient functions and code structure to reduce computational requirements in order
to increase frame rate is called optimization . While Chapter 12 focuses on game optimization, you
will see tips throughout this topic and the Unity documentation aimed at good practices in game
development for better optimization.
Time.deltaTime
You know from videos and movies that you see smooth character movement based on a steady
frame rate. If the frame rate for games varies from frame to frame, you will get choppy, uneven
movement. You just learned that both hardware processing speeds and scripting computations
affect frame rate. What to do?
Time.deltaTime comes to the rescue. “Delta” refers to the Greek letter delta (D), the symbol used in
mathematics to indicate a change in values. In this case, it means specifically the change in time in
seconds that it took to render the previous frame.
For example, velocity, or movement, is a measure of distance/time such as miles per hour. Since
the frame rate varies from frame to frame, for a game object to show a steady velocity the game
engine needs to calculate the change in position per second, not per frame. Time.deltaTime is the
conversion factor used to make these kinds of calculations frame-rate independent , meaning
based on real time.
Scripting in Update
Add the following variable declaration to the others in your script:
public var waitTime : float = 5f;
waitTime is a public variable holding a value representing the number of seconds to wait until the
sphere color changes. Since it is a public variable, you can adjust it directly in the Inspector to
override the script variable declaration assigning a value of 5 to change the time from the start of
play until the sphere turns a random color.
Now add the Update() function code:
function Update()
{
waitTime -= Time.deltaTime;
if (waitTime < 0.0f) {
gameObject.renderer.material.color = Color.blue;
}
}
 
Search WWH ::




Custom Search