Now that we have all of the powerups that we want in place, let’s create a quick feature that will allow the player to collect them from where ever they are. This will allow the player to play a bit safer, but we will look into adjusting how strong it is later on when we allow our enemy units to shoot at the powerups.
To start, we need to go into our player script and create a new gameobject to work with for the player to be able to drag in the powerups:
From here, we can go into our void update and create a new method to work with:
As to how we will make our powerups move towards our player, we will go into our powerup script and have create a code that will have it move towards our player, which we will use in the future again for a new enemy type:
Before we create the movement method however, we first need to create a collection method that will allow us to call the powerups to us. We will also need to create a coroutine that will be called upon to end our powerups movement towards the player after a couple seconds.
Finally, we can create the method in which our powerup has destination when we press our C key:
With our MoveTowards method, our powerups will make the movement to our player. With all of this done, we can now look at how it appears in game.
There we have it, our player can now call upon powerups, however we have to be careful for the pesky Mag powerup, because it will be just as attracted to us as our others.