Game Development Reference
In-Depth Information
//Move cube along forward direction by speed
transform.localPosition += transform.forward * Speed *
Time.deltaTime;
}
}
The deltaTime variable is a floating-point value and always expresses how much
time, in seconds, has elapsed since the previous Update function was called. A value
of 0.5, for example, means half a second has elapsed since the previous frame and so
on. This is useful because deltaTime can act as a multiplier. By multiplying a speed
variable by deltaTime on each frame, we can know how far an object should move
because distance = speed x time . Thus, deltaTime gives us frame-rate independence
for object motion.
Immortal objects
By default, Unity regards every object as existing within the self-enclosed time
and space of a scene. The difference between scenes is like the difference between
separate universes. Consequently, objects don't survive outside the scene to which
they belong; this means that they die whenever the active scene changes. This is
typically how you want objects to behave, because scenes are usually very different
and separate from one another. However, even so, there will be objects that you
don't want destroyed. There will be objects that you need carried over between scenes,
such as the Player character, a high-score system, or a GameManager class. These
are normally high-order objects whose existence shouldn't be limited to specific
scenes; they should rather span or arc across multiple scenes. You can create object
persistence easily using the DontDestroyOnLoad function, but it has important
consequences worth considering. Take a look at the following code sample 3-12:
using UnityEngine;
using System.Collections;
//-------------------------------------------
//This object will survive scene changes
public class PersistentObj : MonoBehaviour
{
//-------------------------------------------
// Use this for initialization
void Start ()
{
//Make this object survive
DontDestroyOnLoad(gameObject);
}
}
//-------------------------------------------
 
Search WWH ::




Custom Search