Platformer: Collectible & Status Bar

Intro

In this article, we will talk about creating a collectible that will disappear when “collected” and increase a status bar. The status bar is going to keep track of how much toxic exposure the player has been exposed to and the collectible is an antidote to heal the player of this toxicity.

The Setup & Code

I covered setting up the Status Bar using an UI Slider element. Check it out below…

Lets talk about the status bar script. In order to gain access to the UI library, we need to declare we are using the UnityEngine.UI.

We will declare 4 variables

  1. Slider class variable that will hold the Slider bar UI element.
  2. An int variable that will be the current toxicity levels of the player
  3. An int variable that will hold the maximum amount of toxicity.
  4. An int variable that will hold the minimum amount of toxicity.

We will assign the Slider (Toxicity_Bar) to the Slider class on the script.

On start, we set the current toxic amount to the min. We set the Slider Bar max value to the max toxic amount and set the min value of the Slider Bar to the min toxic amount. We set the current Slider Bar value to the current toxic amount, which is the min right now.

We will create a public method called Toxic Exposure with a parameter int. We are going to add to the current toxic value the amount of the parameter int then set the Slider Bar value to the current toxic value. This will only happen when the current toxic value + plus the int amount is less than/equal to max toxic value. This prevents it from going over the max value of the Slider Bar.

When the code is complete this will be called anytime when the player has been exposed to the toxic cloud, but for now, we will call it when the player presses the right shift.

We declare a ToxicityBar class variable and assign it in the Inspector.

When we press the right shift key, we call the Toxic Exposure method from the Toxicity Bar and pass in the amount we want to add to the Toxicity bar.

We put the medicine game object into the scene, give it a Box Collider, set Is Trigger true, and give it a tag called Medicine.

Back in the Player script, we use OnTriggerEnter to detect when the player has collided with the medicine and check the tag to see if it is the medicine. If it is we will want to decrease the status bar which represents the toxicity amount and destroy the medicine game object through the Collider parameter “other”.

Lets create the toxic cure method in the Toxic Bar script, so we can adjust the Slider Bar in game when we collide into the medicine game object.

This is a public method called ToxicCure with an int parameter. We subtract the amount designated by the parameter and set the Slider Bar bar value to this new current toxic amount. We only want to do this if the current toxic amount minus the parameter amount is greater than/equal to the mint Toxic amount, so it never goes below 0 or the minimum Slider Bar value.

Conclusion

This whole system will be implemented with the collision of a toxic cloud by the player. When the toxic level is maxed, the player will die. This is going to be the next addition to the 2.5D platform game. Until then…Cheers!

--

--

--

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

Recommended from Medium

Minimax in Clojure (Using Reduce)

A Love Letter to Bricks, or 30 Ways Bricks Runs Circles Around Oxygen

Outlook Calendar meets timeghost

Simple task: CSV structure and data to Snowflake SQL Table.

Version 1.2.0 is available !

{UPDATE} Araña superhéroe escapar policía: hombre de acción Hack Free Resources Generator

Longest Common Subsequence (Top-Down)— Day 52(Python)

You Will Never Get Rid of Legacy Code

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
Ryan McCoach

Ryan McCoach

More from Medium

Platformer: Box Push

My journey becoming a Unity game developer: 2.5D

P3: Creating a Gritty Office Den

What layout works for me in Unity