Unity Tutorial About How To Add Health Bar Above A Character In Simple 2D Game.

Unity Tutorial About How To Add Health Bar Above A Character In Simple 2D Game.

hello everyone in this video I will show you how to add a simple health bar above a character that reduces if this character steps into some dangerous zone and if health is out then character dies scripts will be available by the link in the description let's quickly set up our scene etiquette sprite at rigidbody 2d component to it set gravity scale to zero collision detection to continuous hand free z rotation then add a box Collider 2d that will be a trigger edit it a bit this Collider will detect collisions with fire game object cat game object needs a script to control its behavior here it is here our rigidbody Direction X Direction Y and movespeed variables that help the cat to move then here is health mount variable which is public static in order to be accessible through in other scripts in start method we set health amount as one and get rigidbody component then in update method we'll get values if arrow keys are pressed and designed them to Direction X and Direction Y variables x move speed also here we check if health amount is less than zero and if it is then we destroy our cat game object in fixed update method we'll pass a velocity to catch the rigidbody component according to direction X and Direction Y values if cat collides with something then on trigger enter to the method is invoked where we check if game objects named cat collides with is fire then health amount is reduced by 0.1 that's the script add this grip to cat now let's create a health bar drag and drop white rectangle sprite into cat so this rectangle becomes a child of cat game object rename it as health bar now let's position and scale it properly position X will be negative two point 15 y will be three scale X will be one and scale Y will be zero point one now we need to change health bars pivot point position so it will be at the left side of the health bar game object select white rectangle sprite and set pivot point x value as 0 hit apply to save this change now health bar is about where it should be let's change its color to red to control health bar size so it represents cats health amout we use health bar script it's pretty simple one here we have only one variable which is local scale that is a scientist art method as current scale of our health bar then in update method we set local scale X value as a value that health amount variable of cat script has that's why we need that variable to be public static so using this script and shifted pivot point of health bar sprite we can control health bars appearance this way add this script to health bar game object last thing to do is to add a fire game object as a danger zone at fire sprite and add a box Collider to eat check is trigger option and edit it a bit that's it now we can hit play and see how it works the cat can move around and if it touches a fire then its health is reduced and health bar becomes shorter if health is over then cat is destroyed hope you like this tutorial thank you for watching see you next time


  1. Hi Alexander, great tutorial! I just started my video game creation journey and your videos are really helpful! Just a quick question on this one though, say you had more than one cat? Still applying the health bar to a prefab how would you keep the health unique to each cat? I tried duplicating your method but all of my spawned characters now have identical health bars that do not react individually.

  2. Александр, спасибо большое за ваши видео.
    Я был в шоке когда только открыл ваш канал и увидел количество обучающих видео. Всё самое важное и нужное, очень коротко о главном, без лишних слов.

  3. I just always feel happy when I search for a tutorial on a topic, only to find out you have made one on it. Thanks a lot.

  4. Aight the best I managed to get is a 100% healthbar to 0% health bar instantly and I couldnt fix the problem. Tried many different method and nothing seems to work…

  5. Thanks for the tutorial. I have tried to implement this several times using more complex ways and always failed, and yours was just easy and straight to the point. For those of you that cannot use a static modifier (for example the health bar could be used in multiple enemies, so the variable cannot be shared among them) you can use something like gameObject.GetComponentInParent<Enemy>().enemyHpPoints; to reference each game object health bar and apply Alexander system and would work nicely.

  6. не знаю почему, но линию жизни у меня не видно, слоя менял с бекграундом не помогает(

  7. Привет. У тебя отличные видео. Можешь ли какое нибудь из следующих видео сделать про то , как прыгать по стенам и цепляться за них? 🙂

  8. Thank you so much for your this tutorial you are awesome. and can you please show us hot to make a Gun Like Nonstop ball game ? i add link the below . Thank you 🙂

Add a Comment

Your email address will not be published. Required fields are marked *