Graphics Reference
In-Depth Information
// respawn
Invoke("Respawn",2f);
}
}
Respawn() will reposition the player at its starting position and reset all of the vari-
ables required to put the player back to the state it was at the beginning of the level:
void Respawn()
{
// reset the 'we are respawning' variable
isRespawning= false;
When a player respawns, it may often be placed in a position where there is already
an enemy or some laser fire. It could be unfair to respawn the player and have it
destroyed immediately, so when respawning happens the opportunity for a spawn prob-
lem is alleviated by making the player invulnerable for a short period of time. A call to
MakeInvulnerable() starts this, with an Invoke call to activate the MakeVulnerable func-
tion to counter its effect 3 seconds later:
// we need to be invulnerable for a little while
MakeInvulnerable();
Invoke ("MakeVulnerable",3);
The player mesh was hidden when the player was destroyed, so as it respawns the
SetActive() function is used to show it again:
// show ship body again
theMeshGO.SetActive(true);
As well as the player model being hidden on its destruction, the weapon controller
was disabled. To re-enable it, the weapon slot is set to the first one (zero) to reset it, and a
call to weaponControl.EnableCurrentWeapon() is made to get it going again:
// revert to the first weapon
weaponControl.SetWeaponSlot(0);
// show the current weapon (since it was hidden when the
// ship explosion was shown)
weaponControl.EnableCurrentWeapon();
}
The OnCollisionEnter function will detect when an object first collides with the
player. The layer of the object colliding with it is checked to see whether it is a projectile
(on layer 11) and we make sure that the player is not respawning.
The simple invulnerability system comes into play in this function; the state of
isInvulnerable decides whether or not it should react to the collision.
If all conditions are met, the LostLife() function is called:
void OnCollisionEnter(Collision collider)
{
// MAKE SURE that weapons don't have colliders
Search WWH ::




Custom Search