Easy Enemy Health Bars in Unity
HTML-код
- Опубликовано: 1 май 2023
- The main difference between a Player's Health Bar in the HUD vs an Enemy's is creating the Health Bar in WORLD SPACE.
In stead of overlaying UI on the Camera, the Enemy or Game Object itself has a Canvas attached to it as a child object, and the UI is a part of the Enemy GameObject.
The benefit of this is the Health Bar follows the Enemy around, the downside is we need to make sure it stays oriented and positioned properly as it does.
Learn all about how to solve for that easily here without needing a single asset of your own.
Using Unity's Slider UI component out of the box we can use the guts of that to make a working health bar in close to 5 lines of code. - Игры
It's been too long, bmo. We demand more videos!
I literally never thought of adding health bars directly to enemies, with its own canvas and everything 😅
Thanks for the tutorial ♡
That was short, informative , well explained….. I loved it
Amazing tutorial. There are so many overcomplicated videos on RUclips. You did it simple and straight to the point. Thanks!
Hey so glad youre back.
Love your videos
For anybody who ran into problems with the health bar stretching when rotation is added, I was able to fix this by changing the rotation of the canvas rather than the health bar UI item itself. This is a simple fix:
transform.rotation = camera.transform.rotation;
becomes
transform.parent.rotation = camera.transform.rotation;
hope this helps some people out!
Thank you. Went looking for this comment when I ran into the issue myself. 👍
You are the man, thank you
You are a legend, I was tearing my hair out trying to fix this. Can't believe I fell into the 'rotating a scaled transform' trap AGAIN
a helpful and simple video, and exactly what i was looking for. thank you
Nice, thank you for making this.
welcome back, was just wondering where you dissapared :)
Great - very good teacher
This is so damn helpful!!!! Thank you, thank you, thank you!!!!!!
I'll keep that saved. Thanks!
The King strikes again!
Clicked on this so fast 💨 Bmo is the tutorial rizzler
Very helpful and simple and easy, thank you so much!
Appreciate it, now go make the best game of all time
Wow! You're Back! Welcome I missed your coding videos!😢❤
First, reference the Sprite Renderer next to the variables with "public SpriteRenderer sprite" Then, use code to change the color when the health is under a certain amount. For example, if you wanted it to turn red when under 50 health, you could write "if(health < 50)
{
sprite.color = red;
}
Hope this helped! (Also I've never actually tried it, but if I had to guess, this is how you would do it.)
You are back!
great video thanks bud!!
when it got to the rotation part, when i locked the slider in place, it just started stretching, do you know a fix for this? loved the video!
very very helpfull thanks you.
Nice video man :D
do you have tutorial for that shooting? it looks really good and the way it bounces :D
Very good tutorial. However, I can see the border of Canvas in game view. Not sure why. How to get rid of border of Canvas in game view?
Hi great video loved it small suggestion link in the description or end of video related videos
how do i make it fetch the cameras position on its own and not have to drag it in so when i make a enemy preset i dont have to drag it in to everyone i place down
Thank you!!!
Hey that's a great tutorial, I will use it. A question : My enemies come from far away, how do I keep the health bar big enough to always see it ? Will it shrink as if with the distance ?
does it ever go to zero? looks like there is always a little bit color left
so what if there a multiple enemies? I use pooling for them do I hvae to pool the canvas separately?
Why in the Canvas in the Event Camera you didn't assign the Main Camera ?
One question, if I make too many monsters by prefab in one scene, does that mean it will create tons of canvas? Is that ok?
Also if for some reason you don't want to have a camera variable, Quaternion.identity also works fine though my project is 2D so I dont know if it works in 3D or not.
thanks a lot
Getting an issue on the floating health bar script. On public void update health bar "must declare a body because it is not marked abstract, extern or partial" and on the line below at 5:04 slider, current and max value "does not exist in current context" anyone know where i went wrong 🙏🏾
thanks
This tutorial rotates the whole object to rotate the canvas alongside it. In 3D, your whole model will be facing the floor when rotated, instead of just the model. Doing slider.transform.rotation instead of transform.rotation is an easy fix
Great tutorial!! subscribe.
how to make it turn yellow and red at certain health?
My health bar starts at 0 and then when i shoot it, it goes to 10?
i did it till the increase and decrease teh value, but when i play it, and add background sprite like Mountain or something, the hpbar was covered by the background sprite, how to fix it ?
i already tried to put sprite renderer on the canvas but not work
Same problem, I have health bar on asteroid, but asteroid keeps blocking it
Does anyone know why he uses [SerializeField] instead of the regular private access modifiers?
Shows up in unity inspector
My game Helth bar app avalable