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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Top Myths About Adopting Contract Management Software

2020: The Year of Launches

Explain Decorators in Python?

Xpansion Game Weekly Update

Random numbers are not Random!

Azure AD App registration & Enterprise applications

Microservice Deployment Strategy (Part 2: Serverless Pattern)

All RISE? Reflections on SAP’s Approach to S/4HANA Transformation

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
Niklas Bergstrand

Niklas Bergstrand

Unity / C# Game developer

More from Medium

Fallout 3’s Post-America Society

We Are Early…for Now

True Knowledge is Knowing You Don’t Know Shit: a Conversation with Narque on Art, Decentralization…

Bridgerton Through a Postcolonial Lens