Graphics Reference
In-Depth Information
Earlier in this chapter when we looked at the game controller, PlayerHit() was one of
the functions used to display an explosion effect. Here, we access the static game control-
ler variable GameController_LBS.Instance and call PlayerHit(), passing in this player's
transform:
void LostLife()
{
isRespawning=true;
// blow us up!
GameController_LBS.Instance.PlayerHit( myTransform );
The data manager gets a call next, to reduce the lives count by 1:
// reduce lives by one
myDataManager.ReduceHealth(1);
As the health has changed, we now need to update the UI and tell the user about it:
GameController_LBS.Instance.UpdateLivesP1( myDataManager.GetHealth() );
In this code, the data manager function GetHealth() returns an integer health value.
When health is less than 1, it's time to stop the player from moving (setting its velocity to
zero) and end the game. The player mesh is hidden too (via SetActive):
if(myDataManager.GetHealth()<1) // <- game over
{
// stop movement, as long as rigidbody is not
// kinematic (otherwise it will have no velocity and we
// will generate an error message trying to set it)
if( !myPlayerController.rigidbody.isKinematic )
myPlayerController.rigidbody.velocity=Vector3.zero;
// hide ship body
theMeshGO.SetActive(false);
The weapon system is shut down so that firing doesn't happen anymore, followed by
a call to the PlayerFinished() function to take care of finalizing the game for this player:
// disable and hide weapon
weaponControl.DisableCurrentWeapon();
// do anything we need to do at game finished
PlayerFinished();
If the player's health amount is more than 0, the player's model is hidden, the weapon
system is disabled and a respawn is scheduled 2 seconds from here (via an Invoke state-
ment to call the Respawn() function):
} else {
// hide ship body
theMeshGO.SetActive(false);
// disable and hide weapon
weaponControl.DisableCurrentWeapon();
Search WWH ::




Custom Search