Journal 44 — Player update in Unity, Press C to Collect Power Ups!

Chris Nielsen
2 min readJun 20, 2021

--

In this challenge, we can add a new skill to the player, so that if there’s a power up on screen, the player can press “C”, and the power up will automatically move to the player.

In the Player C# script Update method, we add a check for when the player presses the “C” button, then we create a reference to the Powerup script, and call a function that the player can collect a power up.

Unity C# script for player, check for player press C button

In the Powerup script, we set the bool to true.

Unity C# script for Powerup, bool set to true to collect power up

In the Powerup script Update method, we redo the original downward movement to two different options: If the player has pressed “C”, the power up’s transform position is changed to move towards the player. Otherwise normal movement is downward.

Unity C# script for Powerup, change movement options to either move toward player, or continue downward movement

Then when the power up is close to the player, we start a coroutine to cool down, and also set the collect power up bool back to false.

Unity C# script for Powerup, cooldown

Finally, you can see this in action.

Unity Playmode, collect power up in action

Thank you for your time!

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet