Game Development Reference
In-Depth Information
107 //Hide damage texture
108 ShowDamage = false;
109 }
110 //------------------------------------------------
111 //ON GUI Function to show texture
112 void OnGUI()
113 {
114 if( ShowDamage ){GUI.DrawTexture(ScreenRect,DamageTexture);}
115 }
116 //------------------------------------------------
117 //Function called when player dies
118 public IEnumerator Die()
119 {
120 //Disable input
121 GameManager.Instance.InputAllowed = false;
122
123 //Trigger death animation if available
124 if(AnimComp) AnimComp.SetTrigger("ShowDeath");
125
126 //Wait for respawn time
127 yield return new WaitForSeconds(RespawnTime);
128
129 //Restart level
130 Application.LoadLevel(Application.loadedLevel);
131 }
132 //------------------------------------------------
133 void Update()
134 {
135 //Build screen rect on update (in case screen size changes)
136 ScreenRect.x = ScreenRect.y = 0;
137 ScreenRect.width = Screen.width;
138 ScreenRect.height = Screen.height;
139 }
140 //------------------------------------------------
141 }
Health and Damage: Procedural Textures
Listing 5-6 implemented the bulk of the PlayerController class. This class features a health value,
defined as private integer member health (line 22). Access to this value is controlled through the
public Health property, which validates the health value each time it's updated or changed. When
(and if) it reaches 0 or below, the Player death functionality is executed, as shown in line 87. Notice,
however, that additional functions and variables were added to PlayerController, besides simply
a Health property. Of special significance is the ApplyDamage coroutine, which can be called to
damage the Player. Damage in this sense might seem merely a matter of just reducing Player health,
but typically we want to do more. When the Player is damaged, we may want to play a sound and
flash the screen red to offer graphical feedback that damage has been taken. These effects are not
essential, but they emphasize a point to the gamer that something bad happened. The ApplyDamage
coroutine achieves this effect by fading a red texture into view. Let's examine further exactly how it
does this (see Listing 5-7, which is an extract from Listing 5-6).
 
Search WWH ::




Custom Search