Easy Enemy Health Bars in Unity

Поделиться
HTML-код
  • Опубликовано: 10 фев 2025
  • 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.

Комментарии • 70

  • @TransGuyShane
    @TransGuyShane Год назад +8

    I literally never thought of adding health bars directly to enemies, with its own canvas and everything 😅
    Thanks for the tutorial ♡

  • @tgporter07
    @tgporter07 Год назад +24

    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!

    • @animezeingamer
      @animezeingamer Год назад

      Thank you. Went looking for this comment when I ran into the issue myself. 👍

    • @DPTecas
      @DPTecas Год назад

      You are the man, thank you

    • @MrWebbtang
      @MrWebbtang Год назад +1

      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

  • @theanimationstation9644
    @theanimationstation9644 Год назад +3

    That was short, informative , well explained….. I loved it

  • @eileeng2492
    @eileeng2492 Год назад +3

    Hey so glad youre back.
    Love your videos

  • @rgodoy86
    @rgodoy86 Год назад

    Amazing tutorial. There are so many overcomplicated videos on RUclips. You did it simple and straight to the point. Thanks!

  • @bigp0y
    @bigp0y Год назад +17

    It's been too long, bmo. We demand more videos!

  • @_Garm_
    @_Garm_ Год назад +7

    welcome back, was just wondering where you dissapared :)

  • @kimolsson8564
    @kimolsson8564 Год назад +5

    The King strikes again!

  • @mandamoon9149
    @mandamoon9149 Год назад +6

    Clicked on this so fast 💨 Bmo is the tutorial rizzler

  • @SneezeAlpha
    @SneezeAlpha Год назад +3

    You are back!

  • @evilMissRune
    @evilMissRune Год назад

    a helpful and simple video, and exactly what i was looking for. thank you

  • @MajesticMindGames
    @MajesticMindGames Год назад +3

    Nice, thank you for making this.

  • @pixelart0124
    @pixelart0124 Год назад

    Very helpful and simple and easy, thank you so much!

    • @BMoDev
      @BMoDev  Год назад +2

      Appreciate it, now go make the best game of all time

  • @iiropeltonen
    @iiropeltonen Год назад

    Wow! You're Back! Welcome I missed your coding videos!😢❤

    • @lymincs8031
      @lymincs8031 Год назад

      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.)

  • @globes179
    @globes179 Год назад +4

    Great - very good teacher

  • @Stompin40
    @Stompin40 Год назад +6

    This is so damn helpful!!!! Thank you, thank you, thank you!!!!!!

  • @pixelsoupgames
    @pixelsoupgames Год назад +2

    I'll keep that saved. Thanks!

  • @jacquesduplessis5010
    @jacquesduplessis5010 15 дней назад

    Great tutorial.

  • @benriffe4705
    @benriffe4705 Год назад

    great video thanks bud!!

  • @linsfunbox4052
    @linsfunbox4052 8 месяцев назад +1

    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?

  • @yuxuan130411
    @yuxuan130411 Год назад +3

    does it ever go to zero? looks like there is always a little bit color left

  • @Meireiyangpanmei-g8f
    @Meireiyangpanmei-g8f 6 месяцев назад

    Thank you sir

  • @FRANCOPROHOLA
    @FRANCOPROHOLA Год назад

    Nice video man :D

  • @King_Layze
    @King_Layze Год назад

    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 🙏🏾

  • @katkatgame
    @katkatgame 7 месяцев назад

    Thank you

  • @ProfesserSpheal
    @ProfesserSpheal 2 месяца назад

    I have the health bar working, but how do I make it so that the health bar stays at the top of my game scene and not move with the enemy character?

  • @Johnnys_Manager
    @Johnnys_Manager 2 месяца назад +1

    was hard asf ngl omg

  • @mindbodymusic21
    @mindbodymusic21 Год назад

    Hi great video loved it small suggestion link in the description or end of video related videos

  • @shonsomech7839
    @shonsomech7839 Год назад +1

    do you have tutorial for that shooting? it looks really good and the way it bounces :D

  • @mocheford
    @mocheford 10 месяцев назад

    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 ?

  • @sifou1737
    @sifou1737 7 дней назад

    savior

  • @vovanphat8040
    @vovanphat8040 Год назад

    very very helpfull thanks you.

  • @iamfarruh4eg56
    @iamfarruh4eg56 15 дней назад

    How do I have healthbars placed above each of my enemies (say, I spawn a pack of 10 enemies, and each of them has their own healthbar)?

    • @jacquesduplessis5010
      @jacquesduplessis5010 15 дней назад +1

      add the healthbar to the enemy prefab? so, when you spawn the enemy , it will already have the healthbar attached.

    • @jacquesduplessis5010
      @jacquesduplessis5010 15 дней назад +1

      are you also making a nest of Thorns clone ?

    • @iamfarruh4eg56
      @iamfarruh4eg56 14 дней назад

      @ ahahahahhahaahahah, actually yes, how did you guess?

    • @jacquesduplessis5010
      @jacquesduplessis5010 14 дней назад +1

      @ saw your profile, and took an educated guess XD. let me know if you don't come right :)

  • @marcosrodriguez1590
    @marcosrodriguez1590 Год назад

    Thank you!!!

  • @guillermozafra8993
    @guillermozafra8993 9 месяцев назад

    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

  • @tapcoin5513
    @tapcoin5513 11 месяцев назад

    thanks

  • @spray_cheese
    @spray_cheese 17 дней назад

    [comment response answered my question!]
    I’m confused why you divided current value by the max value. In my case 3 the starting value, and 3 the max value. Get divided and cause a 1 value. Making it so my health bar has 1 hit left, but the enemy has 3.
    If I try to add 2 to the value. It doesn’t update the bar at all.

    • @iamfarruh4eg56
      @iamfarruh4eg56 15 дней назад

      the value of Slider goes from 0 to 1 (0 means slider is fully slided left, and 1 means slider is fully slided right). It means that when your current health is equal to your max health (you are full hp) currentHealth / maxHealth gives you 1. Assigning this 1 to slider value gives you slider that is fully slided to the right. When you are half hp (say 10hp out of 20hp), currentHealth / maxHealth will give you 0.5, assigning this 0.5 to the slider value will give you slider that is slided halfway to the right, etc.

    • @spray_cheese
      @spray_cheese 15 дней назад +1

      @ oh I see, that makes sense now. Thank you for clarifying that for me!

  • @Ryöken17
    @Ryöken17 6 месяцев назад

    looking for you healthbar in children takes too long for the game, you could do it like your slider, drag and drop?

  • @wob3786
    @wob3786 2 месяца назад

    Works for me but leaves an annoying white square artifact i cant seem to remove

  • @Tbout72
    @Tbout72 10 месяцев назад

    Why in the Canvas in the Event Camera you didn't assign the Main Camera ?

  • @TibiaOTarena
    @TibiaOTarena 11 месяцев назад

    so what if there a multiple enemies? I use pooling for them do I hvae to pool the canvas separately?

  • @MohamadNurjaman
    @MohamadNurjaman Год назад

    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

    • @botcomborderreaching3518
      @botcomborderreaching3518 11 месяцев назад

      Same problem, I have health bar on asteroid, but asteroid keeps blocking it

  • @zote1
    @zote1 5 месяцев назад +1

    here is the code if anyone wants it remamber change the name the
    one you are using
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class FloatingHealthBar : MonoBehaviour
    {
    [SerializeField] private Slider slider;
    [SerializeField] private Camera camera;
    [SerializeField] private Transform target;
    [SerializeField] private Vector3 offset;
    public void UpdateHealthBar(float currentValue, float maxValue)
    {
    slider.value = currentValue / maxValue;
    }
    void Update()
    {
    transform.rotation = camera.transform.rotation;
    transform.position = target.position + offset;
    }
    }

  • @superadi2774
    @superadi2774 Год назад

    thanks a lot

  • @Smudge04
    @Smudge04 Год назад +1

    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!

  • @ExileClips123
    @ExileClips123 9 месяцев назад

    My health bar starts at 0 and then when i shoot it, it goes to 10?

  • @Cgtchilly
    @Cgtchilly Год назад

    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

  • @chinesepetants2767
    @chinesepetants2767 Год назад

    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?

  • @danielinverso5869
    @danielinverso5869 Год назад

    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.

  • @YusafAnimations
    @YusafAnimations Год назад

    how to make it turn yellow and red at certain health?

  • @Ryöken17
    @Ryöken17 6 месяцев назад

    We have to rotate the health bar cause you made the enemy rotate, so why rotating him ?

  • @namk0163
    @namk0163 10 месяцев назад

    Does anyone know why he uses [SerializeField] instead of the regular private access modifiers?

    • @BMoDev
      @BMoDev  10 месяцев назад

      Shows up in unity inspector

  • @zaidalfayyadh_2537
    @zaidalfayyadh_2537 Год назад

    Great tutorial!! subscribe.

  • @sourirerouge9995
    @sourirerouge9995 5 месяцев назад

    ty ty

  • @GTN2852
    @GTN2852 11 месяцев назад

    My game Helth bar app avalable

  • @johnnygamer4933
    @johnnygamer4933 4 месяца назад

    You made the Slider slider private, and the you instantiate from unity...
    that's cheating.