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

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!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Securing sensitive data using Cocoapods-Keys

Blackboard to Canvas Migration Final Report

Web Development — The Chamber of Secrets

Skills required in different IT sectors

While free money is being given away through economic stimulus and large amounts of money are…

How to Integrate AdMob Banner Ads with Your iOS Apps using Google Mobile Ads SDK

Launching Chachacha: Rococo’s wilder cousin

MySQL and indexes on char/varchar with UTF-8 encoding

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chris Nielsen

Chris Nielsen

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

More from Medium

Journal 151 — Unity Project Development, 2.5D Shooter

Gamedev Log: “Slash’n’crack” #1 (Unity/C#)

A Series On Creating Immersive Audio in Unity with Custom Wwise Scripts

Spawn Management