Monster shadow

VIDEO

HEALTHBAR in Unity - [Unity Tutorial]

Description:
Create a healthbar for your player character in Unity. Bonus: I'll show you how to smoothly animate the remaining health to give it some cool points! p.s Sorry about the crappy audio on this one!
ADD A COMMENT

Eugene N
Thanks for the nice tutorial! This canvas setup is super easy to use. What do you recommend for maximum # of canvases per scene? My scene could have as many as 30 units perhaps... Thank you again.
Tarodev
30 should be absolutely fine. I remember running 50+ without any performance loss.
Dmitry Wroclaw
Thank you!
Tarodev
You're quite welcome!
syngleton
The quality of your tutorials, both in the sense of how helpful they are and how visually/audibly impressive they are, is phenomenal. Keep making these high-quality tutorials and I've no doubt you'll become the next Brackeys. Great job! 👍
Tarodev
@Caleb Guys stop, you're makin' me blush
Caleb
syngleton could not have said it any better. Love the content. With Brackeys gone you seem like the guy to fill that void. Top tier videos, and you only just stated. Keep up the good work!
Tarodev
That's about as big a compliment you could give me. I grew up on Brackeys. I'll strive to deliver the best content I can, thank you!
srisrid multi purpose channel
You deserve 100 thousand subscribers. When is the discord server coming?
Tarodev
I hadn't even thought about it, but I'll make one now! Stay tuned ❤️
SEE ALL COMMENTS


Transcript:

hey guys so i've got this little guy here and when i click him i do damage to
him and eventually he does but i have no
idea when he's about to die so let's make a health bar so right
click on your player or whatever you'd like to put a health bar on go ui
add a canvas let's call this healthbar canvas
change the random mode to world space change the scale down to 0.001.001
let's zero that out there in the position
and pull it just above his head now for the actual health bar
i've got just this little rectangle sprite here you can use
whatever you'd like as the background and foreground so on the
canvas add an image and call it the background and i'm just going to
slide in my sprite there set native size just so it
sets it to the correct aspect ratio for your for the sprite that you've
chosen and size it to how you'd like and this will
be the background for me so i'm going to make it a darkish color
like that duplicate that and make it a child of
the background and call this one foreground
and let's make this like a nice health barry color
now the important step here is to set the image type to filled
change the fill method to horizontal and as you can see
there it's kind of already starting to look like a bit of a health bar
so now we need a script to control this so let's make a healthbar
script open it up now first thing we're going
to need is a reference to uh um help our foreground
let's just call this health bar sprites then all one eight is just one function
public void update health bar and we'll take in
uh max health and we'll take in [Music]
uh current health as floats and we'll
do health bus bright dot fill amount equals current health divided by
max health like that and then we'll need to actually trigger
this uh so it does something so on my player script i've just got a
simple on mouse down my player will take some damage it will
either die if it's got zero health or it will just take the hit effect
so let's grab a reference to our health bar private health
health bar and then when we first set our current max and
current health we want to initialize the health bar to show the correct value so
let's do update health bar we'll send on our max
health and uh current health and then we'll do the
exact same thing when we get hit just like that
sorry on my player i will send in my health bar
oh we need to first attach the health bar script
and then the health bar requires the foreground script like that
so just put that in now i'll put in my health bar script in my health bar slots
and that should be just about it and there we go so you can see that the
health bar works but it's not exactly what we need for
example it's going the wrong way and also it's not facing the camera so
once it gets kind of like around to the side like this
it's going to be very tricky to see what's going on so i'll show you how to
fix that go back to your healthbar script and in
uh update method let's look into that and we'll set our transform.rotation
equals to quaternion uh look rotation and we'll take
our current position which is our transform position
minus and now we need the camera.main transform dot position
uh now as i'm sure you're aware this camera.main is quite inefficient
so we should never call that uh every single frame so what we'll do is we'll
grab a reference to our camera
and then in start function we'll just say
cam equals to that so we're just calling it once
uh whoops we don't want the full thing just camera.main
and then we'll say cam.transform dot position like that
and that should be it yep so even though that it's spinning
around it's facing us all the time and the health bar is traveling down
the way we would like it one little bonus thing you could do if
you would like it to go down in more of a smooth manner like
you'd like to kind of animate what we can do is we can go back to our
health bar script and we can get a a reference to a target which will
effectively be the where we're wanting to animate our
health bar too and we'll have a new serialized field
here another float and this will be the reduce speed let's
default that's something like two default target to one now what we'll do
is instead of setting our health bar fill amount to that we will
set our target to that and then in our update
function we'll say our bill amount is equal to math f
dot move towards and then we'll basically what we're doing is we're
giving it our current where it currently is and where we want it to be
and how fast we want it to go so current is the health bar fill amount as
it is now target is target and then we'll do our
reduce speed times time dot delta time
there we go back into unity a health bar and let's give that a try
yeah as you can see it goes down smoothly
so there you have it uh if you learned something leave a like subscribe
and i'll see you next time you