Simple power-up absorption

Let’s go over the implementation of absorbing power-ups with a press of a key.

Ensure you have followed this article to at least have one power-up in place.

Open up the power-up script and add a Transform to store the player’s position. Add a float as well for the speed at which the power-ups will move when getting absorbed:

Initialise the player transform in the Start function:

We will check for input in the Update function. In this case I chose the ‘C’ key to hold down in order to move the power-ups towards the player. Using the function Vector2.MoveTowards will ensure constant speed no matter the distance. If you use Vector2.Lerp then the power-up object will slow down as it gets closer to the player:

That’s it. It will already work with these few lines of code.

Good luck!

Unity / C# Game developer