Powerups Magnet- Game Dev Series 49
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.
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.
All done! We can use magnet as Player buff now!
Next: Dodging Time!