Graphics Reference
In-Depth Information
public class AutoSpinObject : MonoBehavior
{
public Vector3 spinVector = new Vector3(1,0,0);
private Transform myTransform;
void Start ()
{
myTransform=transform;
}
void Update () {
myTransform.Rotate (spinVector*Time.deltaTime);
}
}
4.9.1 Script Breakdown
AutoSpinObject.cs derives from MonoBehavior so that it can use Unity's automatic calls
to the Start() and Update() functions:
public class AutoSpinObject : MonoBehavior
{
The function is derived from MonoBehavior so that we can update the object rotation
in an Update() function.
public Vector3 spinVector = new Vector3(1,0,0);
private Transform myTransform;
The spinVector variable is a 3D vector that we will use to spin the object with. By using
a vector, we can spin the object in any direction in the 3D space simply by changing the
values this script component uses in the Inspector window of the Unity editor. Select the
gameObject that has this script attached and change the value in the Inspector to change
its behavior.
void Start ()
{
myTransform=transform;
}
As per every other script, we cache a reference to this object's transform in the Start()
function. Caching this reference will save a lot of valuable processor time throughout the
game.
The Update() function simply rotates the transform, multiplying the spinVector vari-
able by Time.deltaTime to ensure that the rotation is frame rate independent and time
based:
void Update () {
myTransform.Rotate (spinVector*Time.deltaTime);
}
}
Search WWH ::




Custom Search