Powerup Magnet

Our Galaxy Shooter game is getting pretty hectic. There’s a lot of Enemies to dodge, and a lot of Powerups to grab along the way now. But what if we could make it a little easier on the Player, say sacrificing some of their movement so that Powerups would come to them instead!

With a couple lines of code, we’ll make that possible!


The Magnet code

To create the magnet-like code, we’ll be diving into a Powerup script and creating a new function.

We’ll be using the ‘C’ key to activate the magnet, and while that key is being held, all the Powerups on screen will gravitate towards our Player.

Here is the new function:

Now place that new function inside of your Update() function so that the Powerup is constantly checking if the Player is holding down ‘C’ or not.

And that’s really all it is. A few lines of code, and now our Player doesn’t have to run around the whole map to grab a powerup if they are in a sticky situation!

In the next article we’ll be creating one more Enemy, that will have the ability to dodge the Player’s Lasers!



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
Frank Warman

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles