Scripts and tutorial made by TUTO UNITY FR (alias zef) on youtube.
Channel : https://www.youtube.com/channel/UCJRwb5W4ZzG43J5_dViL6Fw
GitHub repo : https://github.com/TUTOUNITYFR/creer-un-jeu-en-2d-facilement-unity
Additional info :
- The healthBar can work with a slider system that uses a gradient. The lines that enable this functionality are commented (disabled). To enable the graident system, here are the steps :
- Uncomment the lines
- Drop "Fill" in the fill in "HealthBar" component.
- Click on the white color over the reference, named "Gradient". It'll open the gradient editor.
- Add your colors. Here's a visual explanation : https://youtu.be/Jcuaxz-ahDQ?t=1795
-
Create a new script called "Item" and paste the script from the github.
-
Add an Image to the Canvas.
-
Replace the image by your borderHealthBar sprite.
-
Create a new empty called "HealthBar" in the Canvas
-
Re-size the HealthBar as the borderHealthBar sprite in the scene.
-
Create border a child of HealthBar.
-
Create an image named "Fill" in the health bar : Right click HealthBar --) UI/Image.
-
Place "Fill" over "Border" in the hierarchy (inspector).
-
Re-size "Fill" so it matches correctly with "Border".
- If you're making a rectangle or squared-shaped healthBar, you can simply do an auto stretch in [Rect Transform] in the inspector.
- If you're making a special-shaped healthBar, resize manually.
-
Add to HealthBar a "Slider" component.
-
Uncheck "Interactible".
Additional intel : Interactible makes the healthBar clickable : for example, if we click on the middle of the healthBar, the "Fill" will stretch itself to the middle. This is why we're unchecking it.
-
Set "Transition" and "Navigation" to "None".
-
Click & Drag "Fill" in the "Fill Rect" fill.
-
Set Max Value to 100. This is your HP max number.
-
Set "Value" to 100.
-
You can now proceed to adding more visual content around your healthBar (like a heart to the left). Don't forget the "Set Native Size" option if your sprites are weirdly shaped. You may anchor your new stuff.
-
You may now re-size your health bar correctly (I mean making it visible and well-placed on your game output).
-
Add a new script to HealthBar named as it ("HealthBar").
-
Copy & Paste the contents of "HealthBar" from the github to your Unity script.
-
Add a new (component) script named "PlayerHealth" to your playable character.
-
Copy & Paste the contents of "PlayerHealth" from the github to your Unity script.
-
Add the references in "PlayerHealth" script.
-
Add to the HealthBar the component reference "Slider" in the fill in "HealthBar" script.