Published in

Nerd For Tech

# Phase 2: Powerup Magnet

In this challenge article, I need to create a system where the player presses a key that would attract all of the powerups on the screen towards the player. And I figured this would be a great way to utilize the energy system that I created.

When the “C” key is pressed by the player, pickups move to the player.

When I created the energy system for my EMP Bomb dubbed the “Orb of Destruction” I wanted it as a separate weapon and not another powerup, but I also wanted it to be hard to get, and this is when I created the energy system where the EMP Bomb would need ten energy to spawn. I figured this new feature would be a great addition to that system where the player needs one energy to use the magnet. And I’ll explain how I did it. But before you follow along, I suggest you read this article first to understand how the energy system is created since I don’t want to cover too much of that in this article.

# The Elephant in the Room

So let’s discuss the biggest challenge of this feature first. When we press that “C” we need to check for every powerup that’s in the scene and tell it to change its direction. There are a few ways to go about getting the objects we want, but since all of our powerups spawn in a parent container called “PowerupContainer” and all we would need to do is get a reference to that container, and then run a foreach loop to get all of the powerups in that container and lastly change the behaviour to those objects. Now at this point, we could either run a function in those powerups to tell it what to do, but since we only need to change its target to the player while the magnet is active I decided to create that logic in the foreach loop.

In that loop, we’ll define a position which is the powerup’s position, and we’ll define a target which is our player. There is something called Vector2.MoveTowards, and what that does is it takes in a current position, a target position and a speed and then moves the GameObject from its current position to the target position at the speed specified. Now, this doesn’t affect the rotation of the GameObjectbut that’s fine for our powerups.

As you can see this is a very easy way to get every object in the parent container that exists and change its movement. And the best part is if the magnet is no longer active then the powerups that remain on the screen will continue to move as it usually does. But this is only a fraction of the logic. It’s time to create the functionality where we can press that C key and let the magnet do its thing.

There are a few ways to get input from the keyboard but I’ve always used the Unity Input system since we can create buttons and reference to those buttons but later we can change the keys assigned if we’re not happy with our selected key. To access these buttons we would go to file, project settings and then select the InputManager. There I created a new Axis called Magnet and added “c” to the positive button. Now we can refer to that button and we’ll be set.

Next, I opened up my Player script and first created some variables. I created a float value for our Magnet speed called _magnetSpeed with a value of 6, I created two additional floats for our magnet duration called _magnetDuration with no value and cooldown named _magnetCooldown with a value of 6. Next, I added a bool called _isMagnetActive and created a GameObject called _powerupContainer that is to get a reference on the actual container.

With all of my variables created, I added a new function called PowerupMagnet(). Inside the function, I created an if statement to get the button using Input.GetButtonDown(“Magnet”), inside that if statement we needed to create another if statement to check if your energy is greater than 0 (1 or more) and also to check if our magnet is not active. Now if those two requirements are met we can activate our magnet. Inside that function we use our AudioManager to play a sound clip to indicate the magnet was activated, we deducted one energy, updated the UI with our remainder energy and added 3 to our _magnetDuration (this is how long the magnet will be active for). Underneath the if statement I created an else statement that would just play an error clip if those requirements weren’t met.

Next, we need to create another if statement, but this one will run independently from the first if statement. In this if statement we check if our magnet is active, and if our magnet duration is greater than 0. Inside the statement, we deduct time from the duration and run that foreach loop that tells all of the powerups to move towards the player. Further down I created another if statement to check if our magnet duration is less or equal to 0 to disable the magnet again and to start a cooldown Coroutine.

With the script and everything that is done, it’s time to test our new feature and you’ll see it turned out really great.

# Conclusion

This challenge was quite fun to do and I loved that I could build on my energy system and I’m looking forward to adding more features in the future using this system even after the challenges and course are completed.

--

--

## More from Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

## Dominique Dos Santos

Self-taught Unity and C# Developer with a passion for games and the stories they tell.