Game Development Reference
In-Depth Information
1.
Select the Slug in the Hierarchy view, and open the Animation view (Ctrl+6) to
inspect its read-only Slug Run clip.
2.
Zoom out on the tracks until you have about 7 seconds showing, and then
turn on the record button and scrub the time indicator.
Watching the Scene view while scrubbing the time indicator, you can see that 6 seconds will give the
slug plenty of time to get through the garden.
3.
Close the Animation editor.
4.
Create a new C# script in the Game Scripts folder, and name it
SlugManager .
5.
Add the following variables:
BatteryHealth batteryHealth; // the script
public float life = 6f; // time to go through the garden
In the Start function, “find” the BatteryLife script and start the timer with a
coroutine:
6.
// Use this for initialization
void Start () {
batteryHealth = GameObject.Find("Battery Life").GetComponent<BatteryHealth>();
StartCoroutine (TimedDestroy()); // start timer for auto destroy
}
7.
Add the coroutine:
IEnumerator TimedDestroy () {
yield return new WaitForSeconds(life);
DestroySlug();
}
The destroy gets called separately because it will also be changing the running flag in the
BatteryHealth script in case you want more than one run, and it will also get called if the slug is hit.
Create the DestroySlug function:
8.
void DestroySlug () {
batteryHealth.running = false;
Destroy(gameObject);
}
9. Save the script.
The console reports that the running variable is not accessible due to its protection level. You will
have to make a small change.
 
Search WWH ::




Custom Search