Graphics Reference
In-Depth Information
Along with the audio, this exploded enemy needs a nice explosion particle effect.
Rather than duplicate code whenever we need an explosion, the function Explode() pro-
vides exactly that:
// play an explosion effect at the enemy position
Explode ( aPosition );
The hitByID variable (integer) tells where the projectile came from. In this game,
when there is more than one player, it is used to tell which player to award the score to.
If hitByID is 1, this is the main user. The score for this hit goes to mainPlayerData-
Manager1, player 1's data manager:
if(hitByID==1)
{
// tell main data manager to add score
mainPlayerDataManager1.AddScore( pointsValue );
The user interface code will display info for one player by default, or for two players
when totalPlayers is more than 1. Here the UpdateScoreP1() function is passed player 1's
score:
// update the score on the UI
UpdateScoreP1( mainPlayerDataManager1.GetScore() );
} else {
As there are only two potential players in this game, the check to see whether player 2
caused this hit does not check against a specific ID. If the ID of this hit is not 1 (as per the
hitByID variable), we assume that this is the second player. The score will be added to the
data manager of player 2, and the user interface update goes out to the UpdateScoreP2()
function:
// tell main data manager to add score
mainPlayerDataManager2.AddScore( pointsValue );
// update the score on the UI
UpdateScoreP2( mainPlayerDataManager2.GetScore() );
}
The power-ups will appear after a certain number of hits by the player(s). In this line,
a counter variable named powerupExplosionCounter, to track explosions, is incremented:
// count how many have been destroyed and if necessary spawn a
// powerup here instead
powerupExplosionCounter++;
When powerupExplosionCounter passes the value of the variable numberOf
ExplosionsToMakePowerup, a power-up is instantiated into the game scene. It is assumed
that numberOfExplosionsToMakePowerup is set or modified in the Unity editor Inspector
window. The Instantiate call uses a prefab referenced in the variable powerUpPrefab, and
it is spawned wherever the enemy hit happened. powerupExplosionCounter is reset to 0
after the Instantiate call:
if( powerupExplosionCounter>numberOfExplosionsToMakePowerup )
Search WWH ::




Custom Search