Game Development Reference
In-Depth Information
64 if(AnimComp) AnimComp.SetTrigger("ShowDeath");
65
66 //Wait for respawn time
67 yield return new WaitForSeconds(RespawnTime);
68
69 //Restart level
70 Application.LoadLevel(Application.loadedLevel);
71 }
72 //------------------------------------------------
73 }
Lines 19 and 22 . The Respawn time variable is effectively a waiting period
used by the Die coroutine. It expresses the amount of time in seconds to wait
before restarting the level, allowing enough time for the death animation to
play. The AnimComp member is an internal reference to the camera's Animator
component; used for controlling and invoking animation states in the Animator
Controller. A reference to this component is retrieved in the class Start event,
at line 33.
Lines 58-72 . The Die coroutine is currently not called by anything in the class. It
will be invoked later when Player health reduces to 0 or less, to invoke the death
animation. When the coroutine is called, the ShowDeath trigger is set in the
AnimationController—notice the name “ ShowDeath ” matches exactly the trigger
name in the Animation Controller. When this trigger is set, the camera death
animation will be played. In addition, the Application.LoadLevel API function is
called to reload the level (respawn behavior).
Note More information in Application.LoadLevel can be found in the Unity online documentation at
http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html .
There are also variations of this function, including LoadLevelAsync and LoadLevelAdditive .
Implementing Health
The Player needs a health feature to keep track of his lifeline in-game. This feature is also important
for determining whether a death and respawn are required. Take a look at Listing 5-6 to see how
PlayerController is refined to implement health and death in full. Relevant changes are highlighted
in bold. Then, in subsequent sections, we'll explore the code deeper to see how it works more
thoroughly. The full PlayerController.cs file is included in the chapter companion files.
Listing 5-6. PlayerController, Life and Death
001 //------------------------------------------------
002 using UnityEngine;
003 using System.Collections;
004 using System.Collections.Generic;
005 //------------------------------------------------
 
 
Search WWH ::




Custom Search