Hazard Collectible in Unity

Objective: Create a Hazard that slows down the player for a short period of time.Note: This is part of a retro-style 2D Space Shooter where power-ups can be collected by the player. This Hazard will behave just like a power-up, although collecting it w…


This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila

Objective: Create a Hazard that slows down the player for a short period of time.

Note: This is part of a retro-style 2D Space Shooter where power-ups can be collected by the player. This Hazard will behave just like a power-up, although collecting it will have adverse effects on the player. As such, this will be following the Power-Up script.

Programming the Hazard

  1. Add global bool variable. This will allow us to toggle the hazard later on.

2. Create a method to handle set the bool value to true when necessary. It’s best practice to create a method to handle bool value changes rather than doing it on the fly. This is also where we slow down the player by halving the speed.

3. Make a coroutine to establish the duration of the hazard. The coroutine below is programmed so that the Player’s speed returns to normal after 5 seconds. The previously created bool variable is set to false so that the program knows it no longer needs to halve the speed.

4. Now that you have a coroutine, it’s time to start it. This will be added to the previously made void method.

5. Call the HazardEnabled method on when the player comes in contact with the hazard. This logic relies on OnTriggerEnter detection. Mine was placed within a Modular Power-Up Script which uses a switch statement to execute methods depending on which object is detected.

The Result:

In the next article, I will be writing about implementing an enemy that can use a barrier/shield!


Hazard Collectible in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila


Print Share Comment Cite Upload Translate Updates
APA

Dennisse Pagán Dávila | Sciencx (2021-08-02T03:05:33+00:00) Hazard Collectible in Unity. Retrieved from https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/

MLA
" » Hazard Collectible in Unity." Dennisse Pagán Dávila | Sciencx - Monday August 2, 2021, https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/
HARVARD
Dennisse Pagán Dávila | Sciencx Monday August 2, 2021 » Hazard Collectible in Unity., viewed ,<https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/>
VANCOUVER
Dennisse Pagán Dávila | Sciencx - » Hazard Collectible in Unity. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/
CHICAGO
" » Hazard Collectible in Unity." Dennisse Pagán Dávila | Sciencx - Accessed . https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/
IEEE
" » Hazard Collectible in Unity." Dennisse Pagán Dávila | Sciencx [Online]. Available: https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/. [Accessed: ]
rf:citation
» Hazard Collectible in Unity | Dennisse Pagán Dávila | Sciencx | https://www.scien.cx/2021/08/02/hazard-collectible-in-unity/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.