Homing Missile Part 3

Mass Homing Missile Power Up

In this article we create a mass homing missile powerup. When picked up it will spawn an homing missile for each enemy that exist currently on screen.

Mass Homing Missile Powerup Prefab

Same logic as creating other powerups. Only difference will be in the code.

Pseudocode Mass Homing Missile powerup pick up

If powerup is picked up
then tell Player to activate mass homing missile
Search enemies by tag
for each found enemy spawn an homing missile
and assign enemy as target to the spawned homing missile

Actual Code in Player Script in our powerup switch statement

case “MassHomingMissile”:
GameObject[] Targets = GameObject.FindGameObjectsWithTag(“Enemy”);
foreach (GameObject Target in Targets)
{
GameObject HMissileObject = Instantiate(HMissilePrefab, transform.position, Quaternion.identity);
HomingMissile HMissileScript = HMissileObject.GetComponent<HomingMissile>();
HMissileScript.AssignTarget(Target);
}

First we get all gameobjects with tag Enemy and put them in an Array. For each object in our array we spawn an missile. On this missile we get the homing missile script and use the public method to assign a target.

Update SpawnManager to make it a rare powerup

Every X seconds the SpawnManager script creates a powerup with a random powerupID. In Start() when powerupID is 5 (Mass Homing Missile) we give it 50% chance to self destruct. Random.value returns a random float number between zero and (very close to) one.

 if (powerupID == 5 && Random.value > 0.5f)
{
Destroy(this.gameObject);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store