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
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
Thank you, you saved me 1 hour of painful debugging.
Real heroes don't wear capes!
THANKS!!!
thankss
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
You are a life saver
Thank You
thank you dude
The health bar does not appear above the character, but directly above it. I just couldn't do it. help please.
@@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!
This was fantastic, just what I was searching for, my thanks to you!
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.
i really like how the health bars aren't visible until hurt, you can make sneaky enemies that way and keeps the screen clean
Godly tutorial ! It works perfectly! Thank you so much 🙏🏻
that is so sweet man!! thank you!
Thank you!
It's really helpful
Thank you so much. Working well.
Loved the Video!
thank you for teaching us :D
Great Working Fineeeee thank U
Thank you so much you saved me
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.
Thank you for the kind words. Throwing a bomb? Interesting topic! We'll make that one next!
Thanks a lot :)
Here you go, as requested : dps.tips/2D_Launchable_Projectiles_YTv1U
Your video is helpful
Thanks mate , that's exactly what I'm looking for
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.
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
Depends on the character, about 0.25 should do for the one in the tutorial
@@DistortedPixelStudios Thank you! That helped!
@@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 !
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...
thx for the link bwooo
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?
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 :/
Try to make canvas an child object.
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.
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.
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?
I can see the healthbar moving and getting damaged in scene view, but the the game its not there.
Dont forget to add "using UnityEngine.UI;" at the top of your script if your slider isnt showing
Thank you! I couldn't figure out why it wasn't working.
MVP
Great tutorial! Simple and straightforward. Thank you 🙏🏾
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.
Thank you >3 !
Does it work on the newest version? Because mine doesn’t
@@FortniteBalls9669 I'm using version 2019.4.24f1 and it's working. What's not working?
Earlier my health bar was just grey
Now that I adjusted the colour thing from 0 and now it works
Hi.
I want to ask why do we need to use Vector3 offset in the script?
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
"Transform" maybe?
noice
().color that .color does not work :? cannot resolve it
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?
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?
you should change the green and red color alpha from 0 to 100
@@fikriar9739 Thx man 👊
@@fikriar9739 Hm, did that as I changed the colors, but did not do it for me, guess I've touched something else.
Ah found it :)
In the slider I had changed the "Fill Rect" to none, changed it and now it's working fine.
:*
Wait did u setup the offset
where is the offset function used? cuz my healthbar sits in the middle of the character
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 ?
Try adjusting the transform of the fill that might work
I also have issue as u. @@ have u fix it ?
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.
Do not place the HealthBarBehaviour script in canvas, but place it as a child of your enemy game object.
@Ahsan Azizan Apa ajg, wkwk
I fixed this by leaving the "Fill (Rect Transform) " in the "Fill Rect" slot on the Slider gameobject.
@@alexfranco2048 how?
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.
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.
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.
Thank you! You saved me many hours. :)
@@flustercucked3392 and I lost 1 hour in this. Thanks for the answear!
@@flustercucked3392 Thank you very much indeed, Opacity hade to change to 255 :) should have read the comments 1 hour ago haha.
Sir please make video to
"Enemy Follow & hit"
I cant drag Canvas onto the healthbar in enemies behaviour???? help please
same problem, i can not see the bar. DO you have the solution?
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?
Maybe you have set the rotation something other than 180?
I have the same problem. Have you found a fix?
How do I make it so my bullets work with this health bar
MY HEALTH BAR IS INVISIBEL HEEEEEEEEEELP
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.
I got the same problem..
when i add more than one enemy they all use the same bar, why?
You copied the object but did not replace the Healthbar reference - makes sure every enemy uses its own healthbar
mine just says the name slider could not be found
which makes no sense please help
it happened to me too! i dont know what to do
edit:
you need to add at the top:
using UnityEngine.UI;
@@ela489 i did that and it exploded even more errors into the console
At the top of your script write "using UnityEngine.UI"
pls tutorial to make the enemy
I need help: My collisions wont work anymore on the enemy. Idk why.
Have someone the same problems??
Well now I've done with this error and I did everything but the health bar is not appearing
try this one----> void update(){
healthbar.sethealth(Hitpoints,MaxHitpoints);
} // put the healthbar... etc. in void update
@@fikriar9739 Healthbar.SetHealth(Hitpoints, MaxHitpoints); do not display the healthbar in void Update. WHY????
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
Did you get a solution for this I'm also getting the same issue
@@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;
}
@@raulaqshal4673 Still getting an error but it kinda works, My health bars are still grey aswell.
@@gabrielbear5268 have u tried to slide the slider? Does your health bar color work?
@@raulaqshal4673 I got it working now without any errors but my health bar is still grey :(
Can someone explain why the healthbar shows up in the middle of the enemy instead of above him?
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.
my health bar do not appear, help me
Why I cant move the enemy?
The tutorial is for health bars. Maybe look up a tutorial for path finding
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 ❤️
same
@@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 ❤️
Hey, I have trouble with the health bars not working on inisated enemies. Any help is greatly appreatiated.
This is not working if your enemy is a prefab and spawning.
my health bar doesnt show up when I run my game
try this one try this one----> void update(){
healthbar.sethealth(Hitpoints,MaxHitpoints);
} // put the healthbar... etc. in void update
@@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
ummm how will the player shoot? you didnt show us
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
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
😮uh bruh its not working health bars just not appearing. The slider is moveing up and down i can see it in the inspector.
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
That means you did not make it a child of the character - check the hierarchy
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
im no pro but try making the "t" in transform a capital
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?
is bc you should put this script healthbar.sethealth(Hitpoints,MaxHitpoints); in void update
You need to hit the enemy first
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
Doesnt even work
My Health bar is just grey, I even adjusted the playback speed and still doesn't work
@@FortniteBalls9669 same
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
If that doesn’t work then just play around with the colour thing
@@FortniteBalls9669 Perfect. Max out A to 255 for it to work
so bad
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?
@@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