2D Character Hitpoints in Unity / 2023

Поделиться
HTML-код
  • Опубликовано: 29 апр 2020
  • In this video we cover 2D enemy health bars for Unity and C#
    This video is part of our "In 5 minutes" series where we show you exactly what you want to know.
    SUPPORT GREAT CONTENT
    ----------------------------------------------------------------------------------------------------
    / distortedpixelstudios
    $3 = Coffee with Perks (You can even get 1-on-1 unity support here)
    CHAT WITH US
    ----------------------------------------------------------------------------------------------------
    Discord = / discord
    Twitter = / dpstudiosdev
    USED UNITY ASSETS
    ----------------------------------------------------------------------------------------------------
    Environment: assetstore.unity.com/packages...
    Characters: assetstore.unity.com/packages...
    Projectile: opengameart.org/content/magic...
    Music by Matthew Pablo
    www.matthewpablo.com
    Track : Stepping Stones

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

  • @Diamedi
    @Diamedi 2 года назад +100

    If your healthbar stays grey go back to where you set Low and High colors in the inspector. RGBA -> A should not be 0, make it 255

  • @lewisgreenshields7210
    @lewisgreenshields7210 2 года назад +22

    For those who are having issues like mine....
    1. There is a video before this titled: "2D Health/ Hitpoints in Unity / 2022"
    Watch that video first.
    2. At 3:46 in this video he very quickly Copy and Pastes a line of code (I missed this)
    Just posting these incase they help anyone

    • @ALPAMA1
      @ALPAMA1 2 года назад +1

      You are a life saver

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

      Thank You

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

      thank you dude

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

      The health bar does not appear above the character, but directly above it. I just couldn't do it. help please.

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

      @@gececiyim87 Been a while since i done the stuff for this video, But mess around with the "y" coordinate for the healthbar. try moving it down or moving it up, until it is just above the head!

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

    This was fantastic, just what I was searching for, my thanks to you!

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

    For those who have the healthbar not attatched to the enemys:
    Change in Canvas Render Mode to World Space, change the width and height to you're screen ratio (16:9 is common so 1920x1080 jadajada).
    And decrease the scale to sth like 0.03 on x and y.

  • @bobvella7228
    @bobvella7228 2 года назад +10

    i really like how the health bars aren't visible until hurt, you can make sneaky enemies that way and keeps the screen clean

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

    Godly tutorial ! It works perfectly! Thank you so much 🙏🏻

  • @Randomguy-op2hs
    @Randomguy-op2hs Год назад

    that is so sweet man!! thank you!

  • @yccccc_pie5650
    @yccccc_pie5650 2 года назад

    Thank you!
    It's really helpful

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

    Thank you so much. Working well.

  • @DrFabio
    @DrFabio 3 года назад

    Loved the Video!

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

    thank you for teaching us :D

  • @nidhinmathew2925
    @nidhinmathew2925 2 года назад

    Great Working Fineeeee thank U

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

    Thank you so much you saved me

  • @Castiell444
    @Castiell444 4 года назад +6

    Your videos are really helpful. I hope you gonna continue this series for a long time. Im trying to make a 2D platformer and your videos helps a lot.Could you make a tutorial by any chance about throwing bomb 💣 . I tried to implement it in my game but for some reason it doesnt work.

    • @DistortedPixelStudios
      @DistortedPixelStudios  4 года назад +4

      Thank you for the kind words. Throwing a bomb? Interesting topic! We'll make that one next!

    • @Castiell444
      @Castiell444 4 года назад +2

      Thanks a lot :)

    • @DistortedPixelStudios
      @DistortedPixelStudios  4 года назад +6

      Here you go, as requested : dps.tips/2D_Launchable_Projectiles_YTv1U

  • @SAHILPathan-bp5sv
    @SAHILPathan-bp5sv 3 года назад

    Your video is helpful

  • @user-gj9vx3vr7e
    @user-gj9vx3vr7e 3 года назад +1

    Thanks mate , that's exactly what I'm looking for

  • @bobvella7228
    @bobvella7228 2 года назад +1

    used this for a 3d game, it worked but if the enemy is behind you it shows the health bar, if they're off screen and to the side of you it works fine.

  • @SergioAlbertoMercadoCelaya
    @SergioAlbertoMercadoCelaya 4 года назад +2

    Great tutorials man! One question about the Offset property... What value did you set for this? If I leave it at 0, the health bar overlaps the enemy's head.. I'm using Unity 2018.4.22f1 btw

    • @DistortedPixelStudios
      @DistortedPixelStudios  4 года назад +2

      Depends on the character, about 0.25 should do for the one in the tutorial

    • @SergioAlbertoMercadoCelaya
      @SergioAlbertoMercadoCelaya 4 года назад

      @@DistortedPixelStudios Thank you! That helped!

    • @paulvolet6622
      @paulvolet6622 2 года назад

      ​@@DistortedPixelStudios I could'nt understand why my healthBar was disapearing when setting the offset value, turns out I was using way too big values, found my mistake with your comment. Thanks for your explananations the video was spot on !

  • @mehdib1781
    @mehdib1781 3 года назад +3

    HELP PLEASE: i can not see the health bar and i put Healthbar.SetHealth(Hitpoints, MaxHitpoints); into the update but it does not show up...

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

    thx for the link bwooo

  • @kingbananagaming
    @kingbananagaming 2 года назад +1

    I tried adding this script to my player and the enemies at the same time just with 2 different scripts with the same code but only the players health bar shows up. Do you know why this is happening? It has something to do with the Camera.main part right?

  • @DustVoltrage
    @DustVoltrage 3 года назад +2

    Any way to implement it using Cinemachine ? I can't find a way to make the heathbars stay in place if I'm using virtual cameras :/

    • @Semga
      @Semga 3 года назад +1

      Try to make canvas an child object.

  • @Pietrofonix
    @Pietrofonix 3 года назад +10

    Hi, i have a problem, first of all I followed the video and everything works, thanks for this, anyway, if I select the enemy with the move tool, i can only move the slider. Furthermore, if i double click on the enemy object, Unity selects the slider, not the enemy. Is there a solution for this? Because it's very annoying.

    • @L3GEND4RYvN
      @L3GEND4RYvN 2 года назад +1

      Following up to see if there is a solution to this. I believe this may be why my health bar isn't displaying? The slider is a child of my enemy & I feel like the position of the move tool is where my Health Bar is displaying.

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

    You did not show the Enemy script, did you make a reference of HealthBar on it? so that you can able to drag an put the canvas on it?

  • @fartedididnot3685
    @fartedididnot3685 2 года назад +4

    I can see the healthbar moving and getting damaged in scene view, but the the game its not there.

  • @ottunmustaan3327
    @ottunmustaan3327 2 года назад +7

    Dont forget to add "using UnityEngine.UI;" at the top of your script if your slider isnt showing

  • @reggiecraig80
    @reggiecraig80 3 года назад +2

    Great tutorial! Simple and straightforward. Thank you 🙏🏾

  • @dragonballz3686
    @dragonballz3686 2 года назад

    Dude I have a problem. I've set the takehit value as 1 but it takes 2 damages. Can you plz say why it happens.

  • @Haotuch
    @Haotuch 3 года назад +1

    Thank you >3 !

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +1

      Does it work on the newest version? Because mine doesn’t

    • @Haotuch
      @Haotuch 3 года назад +1

      @@FortniteBalls9669 I'm using version 2019.4.24f1 and it's working. What's not working?

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад

      Earlier my health bar was just grey

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +2

      Now that I adjusted the colour thing from 0 and now it works

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

    Hi.
    I want to ask why do we need to use Vector3 offset in the script?

  • @RazgyMicYt
    @RazgyMicYt 4 года назад

    Please I need help when I do this i have 1 error in my console that says The name 'transform' does not exist in the current context pls help

    • @wilh7993
      @wilh7993 3 года назад +1

      "Transform" maybe?

  • @kienpc5450
    @kienpc5450 2 года назад

    noice

  • @aliagha.huseynli
    @aliagha.huseynli 3 года назад +2

    ().color that .color does not work :? cannot resolve it

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

    I have problems. When I shoot the enemy it wont work for showing the heathbar. So I hit the enemy and the healthbar do nothing. The enemy just just flying around the screen. The bullet flying too and dont get destroyed
    Is the problem maybe from the shooting or bullet function?

  • @mailmike933m
    @mailmike933m 3 года назад +4

    I have a question... can you tell my why my health bar is showing up in the game view but it doesn't change colors when enemy takes damage?

    • @fikriar9739
      @fikriar9739 3 года назад +5

      you should change the green and red color alpha from 0 to 100

    • @mailmike933m
      @mailmike933m 3 года назад

      @@fikriar9739 Thx man 👊

    • @berringervids
      @berringervids 3 года назад

      @@fikriar9739 Hm, did that as I changed the colors, but did not do it for me, guess I've touched something else.

    • @berringervids
      @berringervids 3 года назад

      Ah found it :)
      In the slider I had changed the "Fill Rect" to none, changed it and now it's working fine.

    • @berkinpaker8582
      @berkinpaker8582 3 года назад

      :*

  • @SojoTaku
    @SojoTaku 3 года назад

    Wait did u setup the offset

  • @Fwsr.
    @Fwsr. Год назад

    where is the offset function used? cuz my healthbar sits in the middle of the character

  • @djgong1329
    @djgong1329 2 года назад +3

    I think I have the most weird issue that no one talks about : without damaging the enemy , a grey bar already show up with a white square at left edge.Then after the first hit, a red color approx 20% hp appears. then Hit one more time, the Hp bar starts working fine and display about 80% of hp. Anyone knows what could be the reason ?

    • @appilz7235
      @appilz7235 2 года назад

      Try adjusting the transform of the fill that might work

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

      I also have issue as u. @@ have u fix it ?

  • @aashishupadhyay5566
    @aashishupadhyay5566 3 года назад +4

    I did the same but when i run the game it shows "object reference not set to an instance of an object" error in my enemy script.

    • @adityarahmanda
      @adityarahmanda 3 года назад +2

      Do not place the HealthBarBehaviour script in canvas, but place it as a child of your enemy game object.

    • @adityarahmanda
      @adityarahmanda 3 года назад

      @Ahsan Azizan Apa ajg, wkwk

    • @alexfranco2048
      @alexfranco2048 3 года назад

      I fixed this by leaving the "Fill (Rect Transform) " in the "Fill Rect" slot on the Slider gameobject.

    • @esternonjohnjoshuadl.2201
      @esternonjohnjoshuadl.2201 3 года назад

      @@alexfranco2048 how?

  • @FortniteBalls9669
    @FortniteBalls9669 3 года назад +8

    I have a problem. My health bar is just grey. I followed the tutorial and did every thing the same. I even adjusted the play back speed. My Unity version is "2021.1.4f1". Any help will be appreciated.

    • @flustercucked3392
      @flustercucked3392 3 года назад +3

      Yeah, I'm getting the same problem. Did everything the video showed, and I have working damage scripts already present. The slider is not updating on damage though.

    • @flustercucked3392
      @flustercucked3392 3 года назад +12

      Figured it out. I assumed the transparency settings on the red and green colors default to solid. They don't. You need to adjust that. I ran into another problem after that - I don't know how his code updates health after damage when his SetHealth method is only in the Start function. I needed to add this into an Update method to get the damage to update properly. Hope this helps.

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +2

      Thank you! You saved me many hours. :)

    • @pinhak
      @pinhak 3 года назад +3

      @@flustercucked3392 and I lost 1 hour in this. Thanks for the answear!

    • @nicklasl7973
      @nicklasl7973 3 года назад +3

      @@flustercucked3392 Thank you very much indeed, Opacity hade to change to 255 :) should have read the comments 1 hour ago haha.

  • @SAHILPathan-bp5sv
    @SAHILPathan-bp5sv 3 года назад +5

    Sir please make video to
    "Enemy Follow & hit"

  • @magdalenapiotrowska4928
    @magdalenapiotrowska4928 3 года назад +1

    I cant drag Canvas onto the healthbar in enemies behaviour???? help please

    • @mehdib1781
      @mehdib1781 3 года назад +2

      same problem, i can not see the bar. DO you have the solution?

  • @uvengine3219
    @uvengine3219 3 года назад +2

    The video is great but I have a problem, when my enemy rotates and move about, the healthbar keeps having a seizure and glitching about, is there a way to solve it?

    • @bofisis4923
      @bofisis4923 3 года назад

      Maybe you have set the rotation something other than 180?

    • @TheExplorerFromEarth
      @TheExplorerFromEarth 2 года назад

      I have the same problem. Have you found a fix?

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

    How do I make it so my bullets work with this health bar

  • @filiprepman1532
    @filiprepman1532 3 года назад +8

    MY HEALTH BAR IS INVISIBEL HEEEEEEEEEELP

    • @alexfranco2048
      @alexfranco2048 3 года назад

      Health bar is invisible until the enemy takes damage. You can create a PlayerAttackScript to inflict damage on the enemy. If you still can't see the health bar, try adjusting the OffSet x and y coordinates on the HealthBarBehavior script (which is attached to the Canvas). I put unity in Play mode and manually adjusted these values while in the Scene tab, then took note when it was visible on the Game tab.

    • @missingno.5024
      @missingno.5024 3 года назад

      I got the same problem..

  • @sadouzz4527
    @sadouzz4527 3 года назад +1

    when i add more than one enemy they all use the same bar, why?

    • @RiaanWalters
      @RiaanWalters 3 года назад +1

      You copied the object but did not replace the Healthbar reference - makes sure every enemy uses its own healthbar

  • @isaacwilkerson4057
    @isaacwilkerson4057 3 года назад +4

    mine just says the name slider could not be found
    which makes no sense please help

    • @ela489
      @ela489 3 года назад +3

      it happened to me too! i dont know what to do
      edit:
      you need to add at the top:
      using UnityEngine.UI;

    • @soft8819
      @soft8819 3 года назад

      @@ela489 i did that and it exploded even more errors into the console

    • @nuno97teixeira
      @nuno97teixeira 3 года назад

      At the top of your script write "using UnityEngine.UI"

  • @chendeborah3945
    @chendeborah3945 2 года назад

    pls tutorial to make the enemy

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

    I need help: My collisions wont work anymore on the enemy. Idk why.
    Have someone the same problems??

  • @RazgyMicYt
    @RazgyMicYt 4 года назад

    Well now I've done with this error and I did everything but the health bar is not appearing

    • @fikriar9739
      @fikriar9739 3 года назад +2

      try this one----> void update(){
      healthbar.sethealth(Hitpoints,MaxHitpoints);
      } // put the healthbar... etc. in void update

    • @mehdib1781
      @mehdib1781 3 года назад

      @@fikriar9739 Healthbar.SetHealth(Hitpoints, MaxHitpoints); do not display the healthbar in void Update. WHY????

  • @raulaqshal4673
    @raulaqshal4673 2 года назад +1

    Hello sir. I have an issue
    "object reference not set to an instance of an object.
    Healthbarbehaviour.Sethealth (Single health, Single maxHealth)"
    Can u help me? I don't get it what's the problem

    • @gabrielbear5268
      @gabrielbear5268 2 года назад

      Did you get a solution for this I'm also getting the same issue

    • @raulaqshal4673
      @raulaqshal4673 2 года назад

      @@gabrielbear5268 i changed the code to be like this ::
      Public Slider slider;
      Public void SetMaxHealth(float health, float maxhealth)
      {
      slider.gameObject.SetActive(health < maxhealth);
      slider.value = health;
      slider.maxValue = health;
      }

    • @gabrielbear5268
      @gabrielbear5268 2 года назад

      @@raulaqshal4673 Still getting an error but it kinda works, My health bars are still grey aswell.

    • @raulaqshal4673
      @raulaqshal4673 2 года назад

      @@gabrielbear5268 have u tried to slide the slider? Does your health bar color work?

    • @gabrielbear5268
      @gabrielbear5268 2 года назад

      @@raulaqshal4673 I got it working now without any errors but my health bar is still grey :(

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

    Can someone explain why the healthbar shows up in the middle of the enemy instead of above him?

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

      When he adds the HealthBehaviour script to Canvas at @3:49, the x, y and z fields appear below. You can move it up-down or right-left by entering values in the x and y fields there. I had the same situation, I was able to move it up by entering 0.4 in the y field.

  • @zuhalrobbani3852
    @zuhalrobbani3852 2 года назад

    my health bar do not appear, help me

  • @unknownguy2092
    @unknownguy2092 3 года назад +2

    Why I cant move the enemy?

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +2

      The tutorial is for health bars. Maybe look up a tutorial for path finding

  • @ani-gamer320
    @ani-gamer320 3 года назад +4

    Sir after decreasing health I've an object.
    When an player collide with that object
    The players heath should get increase
    Sir I need script for that please help me❤️
    Loved your video ❤️

    • @dantepants7454
      @dantepants7454 2 года назад

      same

    • @ani-gamer320
      @ani-gamer320 2 года назад

      @@dantepants7454 it's easy buddy just do OnCollision enter or trigger and health ++ (if health is in certain amount then health +=10) that's set ❤️

  • @Zhisaoka
    @Zhisaoka 2 года назад

    Hey, I have trouble with the health bars not working on inisated enemies. Any help is greatly appreatiated.

  • @muhammadusama8111
    @muhammadusama8111 2 года назад +2

    This is not working if your enemy is a prefab and spawning.

  • @teck9162
    @teck9162 3 года назад +2

    my health bar doesnt show up when I run my game

    • @fikriar9739
      @fikriar9739 3 года назад +3

      try this one try this one----> void update(){
      healthbar.sethealth(Hitpoints,MaxHitpoints);
      } // put the healthbar... etc. in void update

    • @kitoo930
      @kitoo930 3 года назад

      @@fikriar9739 how about my transform.parent error what's wrong with that? and how i can fix that? I've followed his tutorial and got underlined message transform.parent error and when i try to fix that show up other error,Help me sir

  • @peteypeetepete
    @peteypeetepete 3 года назад

    ummm how will the player shoot? you didnt show us

  • @chinest3386
    @chinest3386 2 года назад

    When I tried it disappeared I had no problems and followed the video and copied everything I’m new to making games and this is really important to me please help

    • @bobbville
      @bobbville 2 года назад

      I had a little trouble with this too, make sure your scripts are placed in the right area!
      I'm not really sure what issue you're having but try running the game and see in the inspector if the enemy health is going down every time you shoot. If not, then you might need to change something around in your script

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

    😮uh bruh its not working health bars just not appearing. The slider is moveing up and down i can see it in the inspector.

  • @marcolizzi4980
    @marcolizzi4980 3 года назад

    Hi, I have a problem, when I move the player, the enemy's health bar follows me, it doesn't stay "attached" to the enemy

    • @RiaanWalters
      @RiaanWalters 3 года назад

      That means you did not make it a child of the character - check the hierarchy

  • @yliasfranckeric5720
    @yliasfranckeric5720 3 года назад

    Hello i get this error on the health bar behaviour:
    An object reference is required for the non-static field, method, or property 'Component.transform'
    please help me

  • @davidt.legoanimation8274
    @davidt.legoanimation8274 3 года назад

    I copied your tutorial but I still have a problem. If I run my game i don't see the healthbar at all. Does someone know what the reason could be?

    • @fikriar9739
      @fikriar9739 3 года назад +2

      is bc you should put this script healthbar.sethealth(Hitpoints,MaxHitpoints); in void update

    • @yxnautilus8804
      @yxnautilus8804 3 года назад +2

      You need to hit the enemy first

    • @user-bb5xh1en5c
      @user-bb5xh1en5c Год назад +1

      problem this
      Slider.transform.position = Camera.main.ScreenToWorldPoint(transform.parent.position + Offset);
      This move slider position left bottom. Not see camera. This see duble click inspector in the run scene

  • @xXalimousiosXx
    @xXalimousiosXx 3 года назад +2

    Doesnt even work

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +2

      My Health bar is just grey, I even adjusted the playback speed and still doesn't work

    • @xXalimousiosXx
      @xXalimousiosXx 3 года назад +1

      @@FortniteBalls9669 same

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +1

      I found a solution: Go to the canvas and inside the canvas you should see the health bar script. Click on the colour thing and make sure the A part is not 0 this is what makes the health bar grey. Hope this helped

    • @FortniteBalls9669
      @FortniteBalls9669 3 года назад +1

      If that doesn’t work then just play around with the colour thing

    • @Diamedi
      @Diamedi 2 года назад +1

      @@FortniteBalls9669 Perfect. Max out A to 255 for it to work

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

    so bad

  • @soulfate2996
    @soulfate2996 2 года назад +1

    I keep getting the error:
    (NullReferenceException: Object reference not set to an instance of an object
    HealthBarBehaviour.Update () (at Assets/Scripts/AI/HealthBarBehaviour.cs:30))
    On this line of code:
    slider.transform.position = Camera.main.WorldToScreenPoint(transform.parent.position + offset);
    Can anyone help me figure out whats going on?

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

      @@AhmetBabagil. I don't recall exactly but I believe The object reference not set to an instance of an object error comes from you for getting to go into unity and assign the script to the object