Powerups Magnet- Game Dev Series 49

S.J. Jason Liu
Jun 14 · 3 min read

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

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

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

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

From Confusion to Clarification

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

Written by

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

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