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


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

Leave a Reply

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