Game Development Reference
In-Depth Information
What does this script do is simply handle OnHealthIncrement and OnHealthDecrement
messages by taking the new health value as interpolate value between zeroHealth and
fullHealth colors. Since the health is represented as integer value, it must be converted to
a float between zero ( minHealth ) and one ( maxHealth - minHealth ). Finally, Color.Lerp()
is used to set the new color value. If you play the game after attaching this script to player's
prefab, you can notice that the cube starts in green. As the player receives hits and the
health drops, the color changes to yellow, orange, and then red.
The next question to answer is: what happens when the player dies? Currently nothing,
since we do not do anything when the health of the player reaches zero (or less). However,
what needs to be done is to take one life from the player and regenerate it again with full
health. Therefore, we need to appropriately handle OnPlayerDeath that PlayerHealth sends
when the player dies. This must be handled by an external script that counts player's lives and
manages game state accordingly. In other words, when the remaining lives reach zero, the
game is over and no further regeneration is possible. This script is LivesManager , and it must
be attached to a permanent object in the scene. From a logical point of view, it is not
possible to attach this script to the player cube game object, since the destruction and regen-
eration of this object causes stored values to be lost. One good option is to attach this script
to the main camera. LivesManager script is shown in Listing 77.
Search WWH ::




Custom Search