How to create a simple UI health bar for a character in Unity game. Easy tutorial.

How to create a simple UI health bar for a character in Unity game. Easy tutorial.



hello everyone in this video I will show you how to create a simple health bar for your unity game first let's create new UI image game object and put health bar background sprite to the source image slot of its image script component double click on image to get a focus then check preserve aspect option position our image at the top center holding alt shift keys make our image bigger by changing its weak I rename this game object as health bar background then right-click on health bar background and create new UI image name it health bar put healthbar sprite to source the image slot of image script check preserve aspect option and change health bar width so it's a bit smaller than a backgrounds one and curl it to the top center then focus on it and correct its position then go to image script component and change image type from simple to field after that change fuel method to horizontal now if we direct the field amount slider then we can see how our image changes we're gonna change fill amount value through the script okay get back to the scene view let's add a player and the damage zone at rigidbody2d component to the player with gravity scale set to zero and collision detection set to continuously then add a circle Collider edit it to fit the shape of the sprite then add a circle Collider to the skull and check is trigger option time to add some scripts let's take a look at health bar script first here we have image health bar game object float max health variable which equals 200 and public static float health variable in stock method we get health bar image component and set health variable 200 and in update method we simply set a fill amount to health value divided by max health put this drip to health bar game object next let's take a look at the move script for our player it is simple movement script that allows us to move our player with a rigidbody component attached by pressing arrow keys at this trip to our player the Scout script is the easiest one here we have only one method which is ontriggerenter TDD so if something collides with the skull then health variables value of health bar script is decreased by 10 put this trip to the scout game object everything is done let's hit play and see how it works player collides with the skull and health is decreased thank you for watching see you next time

27 comments

  1. Hi everybody! Please share this video if you like it! I will be very appreciated for that! Thanks for watching)

  2. i have a platform with a box collider when i make my enemy to istrigger the enemies just fall right through the platform how do i fix this

  3. Quick and easy even for a dumb doll, very well done, in fact i spent more time writing this comment here than actually understanding what the tutorial is about.

  4. thanks for the video. Finally an explanation thats easy, fast and actually 100% works (unlike 99% of the other vids about this topic).

  5. You can actually create your background image, and then create a child image of what it would look like empty. Let's say you create a dark red back ground with a black empty image. you make the black empty image your backgrounds child object. (Set native size and resize each time). Then you want a green image for your full bar, which will be your full health bar. (For example)
    Give your black empty image a component called Mask. Then make your full bar a child of that. You wont need to do the resizing that you see here. Not taking anything from Alexander Zotov because the man is a genuis! I'm offering an alternative that I find to be much more efficient.

Add a Comment

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