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!

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!

--

--

--

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

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

Recommended from Medium

Designing and writing a Control Plane for distributed services

How to estimate cloud consumption

Keep your exclamations safe

Which API is right for your Slack app?

What is Kubernetes and how to use it?

Containers

Restricting Movement and Pseudo Code

Stellar Dev Digest: Issue #48

Water Connects The World

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

More from Medium

ESP 32-Project 4: External Sensor

Enemies Straight Ahead!

Unity Performance Optimization Ⅴ: Lua Scripts Optimization

How to upgrade materials to URP in Unity 2021.2 and above