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/.

Powerups Magnet- Game Dev Series 49

S.J. Jason Liu
Nerd For Tech
Published in
3 min readJun 14, 2021

--

Objective: Create a powerups collect function with pressing C key.

While our enemies are getting stronger, the Player still got no upgrade. It is a good time to cheer up our Player!

In this article, we will create a magnet to collect all the powerups in scene with pressing C key. It will contain 3 parts with this function.

  • Create visualize slide as magnet cooldown system
  • Press C to collect all when magnet is ready
  • Change the movement of powerups

Visualize magnet

Let’s create a Slider in Canvas and adjust the value, which is almost the same setting as the Thruster Recharger.

Open UIManager and create the function of adding slider value with a new variable.

The bool “_isCollecting” is what we are going to control the slider value from Player. Therefore, we need to create a public method to control it.

Next, let’s create the function of magnet in Player.

Magnet function in Player script

Since there might be more than 1 powerup in scene, we should create an array variable to storage all the powerups when show up.
Then we can call the powerups in Update().

Then we can create another bool to enable the magnet function.

And also call the public method from UIManager to enable it.

Add lines in UIManager script.

That would be all done with function. Then we need to make the powerups move toward Player.

Powerups movement

Let’s also create a bool in Powerup script. Then create a public function that allow us to adjust the bool from Player.

Then we can add an if statement in Update().

Use MoveTowards like what we did in the previous articles.

Then we need to add one more line in our Player script to access it.

Add a line in Update() of Player script.

All done! We can use magnet as Player buff now!

--

--

Nerd For Tech
Nerd For Tech

Published in 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/.

S.J. Jason Liu
S.J. Jason Liu

Written by S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

No responses yet