Homing projectile power-up

Let’s have a look at the implementation of a homing projectile power-up.

Set up the power-up system following these two articles: first and second article.

Open up the Powerup script and add another value to the PowerupType enum:

Open the Enemy script and add a getter to the _isDead bool to ensure it’s possible to check the state from other scripts:

Open the Projectile script and add two private bools: one for checking if this is a homing projectile, and the other for checking if the projectile has a target:

Add a function to look for the nearest target. Explanation is in screenshot comments:

Call this function from the Start function via InvokeRepeating to prevent having to run this costly check on every frame:

Update the MoveProjectile function to move the projectile towards the enemy(homing) if it has a target. Further explanation is in the screenshot:

Open up the Player script and create a public enum called FireMode on the bottom, outside of the class. Name the values based on the three types of fire mode:

Add a private FireMode and GameObject for the projectile prefab to the class:

Add another case to the switch statement for the new power-up and set it to start a coroutine similar to the other power-ups:

Swap out the if statement in the Shoot function for a switch statement:

Open up the SpawnManager script and update the coroutine SpawnPowerUpRoutine to make the new power-up spawn less often:

Lastly, add the power-up to the SpawnManager and projectile object to the Player via the Inspector:

Final result:

Good Luck!

Unity / C# Game developer