Powerup Magnet

Frank Warman
2 min readJun 1, 2021

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!

ALL THE POWERUPS!

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!

--

--

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