Easy Enemy Health Bars in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm going to show you guys how you can take an enemy like this one here and give them a health bar like this the health bar updates as it takes damage and it will always be positioned over the enemy and facing towards the camera which means this works in 2D as well as 3D and the best part is you don't need any pre-made assets or anything like that everything works out of the box in unity so I have my project here and in my scene I have my player and I have this red box which is an enemy we're going to start in 2D but again this is gonna work for both this enemy has a few components on it but none of them are relevant to the tutorial at hand the only thing that might be interesting is this enemy script has our health and Max Health stored on the anime script but otherwise this really just handles like how the enemy chases the ram which I followed in previous tutorials you don't need to do any of that you just need a health and a mag's health so the real difference between say a health bar above our enemy and a health bar that's on our player that's on like the HUD right is that we actually want this health bar to live in World space and that's really easy to do so you can make your health bar with images or whatever you like but I actually like to use the slider UI component that comes with unity and I'll show you how to do that so I'm going to right click on my enemy here and I'm going to go to UI and pick slider I'll rename this to health bar and you'll notice this also attached a canvas on our enemy gamma object so let's go ahead and click our canvas and we'll change the render mode from screen space overlay to World space and this is very important and so what we can do on this canvas is right click Rec transform and hit reset and this will position it over our anime and you'll notice if we double click it it's quite large right now it's actually like way too large and so you could actually just shrink in the sides if you want another thing you could do is go to the scale in the rec transform and just make each axis 0.01 or something like that and so now it's actually funny enough it's the perfect size of our rectangle we can actually make it even smaller it doesn't really matter I'll just zero it out in the X in the Y position and then drag it above the player so we have this canvas the size doesn't really matter it's just the positioning of it there's no reason for it to be big that's that's why we're shrinking it down and now we have this health bar slider that's attached to the canvas it's being rendered in World space and so the slider gives us things we don't need and the first thing I'm going to do is uncheck interactable because this is just going to be a display we don't want to interact with it and then we have this handle slide area that we can just delete entirely and so now on our slider component we can mess with this value and you'll see it's increasing and decreasing right the one issue we have is when our value is set to 1 which is the maximum this would be like a full health bar it's not reaching all the way so I'm just going to click on fill area and then using our rect tool drag this out until it reaches the end and there you go we now have a health bar that will go from zero all the way up to maximum we can then expand fill area and click on fill this is the actual image of the health and I'll just make this like a light green or something just to understand what's going on and that's it that's all we really need at this point you could like expand it out like let's make it a little thicker in the y-axis and there you go we have a health bar above our enemy feel free to make it look however you want this is just a very simple way using a UI component that Unity gives you out of the box all right so our health bar at this point is basically done stylistically so let's go ahead and create a new c-sharp script and I'm going to call this floating health bar and for me that's just going to signify that we want to use this in World space and then I'll go ahead and attach it to our health bar that's attached to our enemy okay so I'm going to get rid of the start method and the first thing you want to do is create a couple variables the first one we're going to make is a private slider and you'll see it's not actually going to pop up that's a slider joint 2D which we don't want this is just because we need to add a using Unity engine dot UI library at the top of our script and so if we actually look in the inspector we see that our health bar has a slider component and that's because we're using a Unity slider right and the field we want to access is actually called value this is the value you want to change that actually increases and decreases the display of the health bar so if we were to say slider.value you'll see that we actually can access this directly which is great so what we want to do is make a public void update Health Bar Method and in terms of arguments we really just need two float variables one for current health and one for Max health or really what any values you want to provide to this bar doesn't have to be health so again on my enemy script I have a health and I have a Max Health this is an area that's going to be catered to your game however you have it set up you do not have to follow my lead and so I could enter something like an enemy script right and that way I could reference the health and Max Health but this is actually not a great idea because then we are tying this floating health bar component directly to an enemy and what if we wanted to use this above the player or above you know some in-game objects or like anything else so that's not usually a good idea it's much better to just provide it with some generic float values so what we can do is say float current value and Float max value and this way it's a little more generic and just like before all we really want to do is say slider that value is equal to the current value divided by the max value that's it and we actually don't even need to call this a floating health bar it could be like a floating status bar used for stamina or mono or anything else in your game right but we'll just go with this for now and where we actually want to call update health bar could be in our enemy script that's where our health is right that's what we can provide to it so I can make a reference to a health bar component or a floating health bar component sorry what I'm going to do just for convenience is get the component in awake so I'll say health bar is equal to get component in children and look for a floating health bar and so down here in my enemy script I have this take damage function where we provide a damage Mount and then we reduce our health by that your game is probably going to be set up differently wherever this value of this bar is being adjusted whether that's in a take damage function or anything else you'll have to find that out and you'll have to have two float values to provide for me it's right here and all I want to say is healthbar.update health bar which is the function we made and provide our health and our Max health so as long as we have a reference to this health bar we can call this function and provide some values it's that simple it should be easy to adapt to your game and so we're calling this intake damage but we also could just call it up and start after we set our health just so every time we restart the game it refreshes as well last thing we have to do to test this out is on my health bar under the enemy we require a slider and so I could just do this like we did in a wake to fetch it but I'll just go ahead and drag the slider in and because we're calling it a start I could change this value for testing now and when I hit play we should see it fill right up you'll immediately notice that something's off here regardless as we start to shoot our anime the health bar goes down right and then it disappears and dies so it is working but we need to solve for now the health bar not really staying in position and rotating with the Enemy as it moves around but don't worry we can fix this really easily back in our health bar script I'm going to make two more variables I'm going to make a variable for a camera and I'm going to make a variable for a transform I'll call the transform Target so we have a camera variable and a Target down here in update really what we want to do is set the rotation of this health bar to match the rotation of our camera because right now what's happening is I can click on this health bar and I can click on rotation and start changing it right and that's what's happening the health bar is rotating with the Enemy but really what we want to do is always have it the exact same rotation as the camera we have in this case it's main camera and that way it'll always look straight on and that's really easy to do we can just say transform dot rotation is equal to camera.transform dot rotation so I could go to our health bar and drag in our main camera and so now at least the health bar is always facing towards the camera right it's not rotating anymore that issues fixed the second problem now is we need the position to always be locked above the enemy so we want it to be positioned like here again on our health bar we have this empty Target variable for a transform we can just click and drag our enemy into it and from here what we can do is actually get the target.position right which is the enemy position and so if I set the transform dot position is equal to the Target dot position it should be close but really we're just going to be set into the middle of the enemy all the time which maybe this is what you want if you have a top-down game but if you wanted it to be say positioned above the enemy or always offset by a certain amount well then it's really easy what we can do is make a serialized field of a private Vector 3 and I'll call this offset and now in our component you'll see we can actually put in a value so I'm just going to add 1 to the Y value and leave 0 and x and 0 and z and going back to our script we just want to say transform position is equal to the Target position plus offset and this way we can actually configure where this health bar is going to be but now when we play we have an offset of one in the y-axis we could see that regardless of where our enemy moves or how he rotates it's always going to be positioned in the same exact spot and for me this is what I wanted so we are done and as promised in a 3D environment it's the exact same thing the health bar will always look towards us and it will always be positioned based on the offset you create so you can play around with that offset in your game to get it perfectly where you want it that's it for the tutorial I hope this helped you out leave a like if it did and I'll see you guys in the next one foreign
Info
Channel: BMo
Views: 46,952
Rating: undefined out of 5
Keywords: bmo, unity, tutorial, unity tutorial, enemy health bar, health bar, unity enemy health bar, world space health bar, unity world space health bar, how to make an enemy health bar, healthbar, bmo unity tutorial, 2d health bar, 3d health bar, 2d game, 3d game, unity 3d tutorial, unity 2d tutorial, unity easy health bar, unity health bar tutorial, unity enemy health bar tutorial, unity 3d health bar, unity 2d health bar, beginner tutorial, c#
Id: _lREXfAMUcE
Channel Id: undefined
Length: 9min 25sec (565 seconds)
Published: Wed May 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.