How To Build an Enemy Health and Damage System in Unreal Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome here welcome back to the channel where I make how-to videos from subjects like mac tips backhand Swift Salesforce and game development this is another Unreal Engine tutorial in this video we're going to learn how to build an enemy health and damage system in the last video I showed you a player health and damage system so this time we're gonna do one for the enemy let's go so I'm using some assets from the marketplace the goblin you can check that out so it's currently free for the month so with the goblin asset I have two AI characters that I created here with which has a spear on them when I hit play here you would see that they have health stats each one of them have different Health stats and with my character overlaps near the AI it would decrease their health and eventually they would versus blame and die so similar here with the other character same thing cool so let's go ahead and see how to build this so starting off with the blank third person template I'm going to go to my contact drawer first thing I want to do is organize so I'm going to create a new folder called Main okay couple of folders here called bloop one is blueprint and the other one is called UI so there's a UI folder right click and select user interface and select widget and user widget so I'm going to call this wbp enemy health and I'm just gonna quickly do this so first add a canvas then add a progress bar progress bar make sure it's anchored in the center and then reset the spacing here for the anchor I'm going to say minus 52 and minus 20 and size y20 and then I'm going to add text here and same with the text here I'm going to name this um current help text in the current health text it's going to be -48 minus negative 52 and size 16. I'm just gonna quickly duplicate this with Ctrl D Ctrl D for this second one this is just gonna be a forward slash it's not important but I want the anchor to be the same as the rest so um so this is -8 minus 52 cool and then this one same thing anchor there reset everything and three minus 52 and tax of 16 shows should be fine cool so we want to create a couple of why is it up there let me do that again sorry uh minus 52 minus 20. index of 16. there you go those were just some rough numbers that I played around with you could figure out what works best on you so here I'm gonna create a binding so right here click on great binding and we're going to create a couple of variables make sure you're creating on the variable not on the local variable so this one I'm going to call it current health with a value type of integer so I'm just gonna quickly return that value fix that a little bit and go back to the designer go here to the next one of under attacks click on bind and create binding drag that out we'll create another variable here called Max health and same thing we're going to return the max health compile save now finally we could create the progress bar so the progress bar at the moment is a float so because it's a float we need to come makes a few adjustments so great uh click that and click on great binding and here what we want to do is when we're going to grab the Grant health and the current Max health and basically we're gonna change this to float same here two float and then we're gonna divide the current health with the max health to get the float returned to us so that's how we get the percent click on Save and yeah and that should be it next is let's go back to the graph actually and go under the event graph so we don't need any of this uh currently so you could delete that and instead search for add custom event and we want to say update Health here and from the update Health we're going to have an input so for the input I'm going to call this current health with a type of integer integer there you go and then we're gonna set the current health to whatever value is passed there so there you go so that's how you set up the widget let's compile save and then go back to our content drawer actually and go under the blueprint so here right click select blueprint class and select character so I'm going to say BP um Main enemy and let me drag that in here cool let me quickly save that so under the mesh other skeletal mesh assets here you could search for SK Goblin if you're using the contact pack and I can use this and I could just roughly put them down here and let me add them here and let's see if it's not floating it is floating so that should be fine uh anyway not gonna spend too much time there anyway next is we're going to add a spirit Collision cool so make sure that Spirit Collision is a bit bigger than the character and then go back to the mesh character here and click on ADD and search for a widget and then drag that widget a little bit higher on top of the character like so and under the details here look for the widget class and the user interface and select for the widget that we just created WB enemy health and currently it's just shown on One Direction here instead of always visible so I'm gonna change this space to screen compile save I'm going to go here and hit play you can see that the character is now showing some health stats apparently the character is not animating let's go back there select the widget under here part of the contact pack there's a um there's some animation already so I'm just gonna select animal Goblin body Idol so that should animate the character a little bit give it some something next is let's create a couple of variables so here they're here I'm gonna say current health and give it an integer then say Max Health give it an integer well I'm gonna jump into the event graph and delete couple of these we only need the event beginplay so I'm gonna grab the widget here drag it out and then I'm gonna check get user widget object and from here I'm gonna cast to the wbp enemy Health widget that we just created hook that up like so and from here I'm going to create promote this as a variable I'm gonna call it wbp health like so and then we're gonna set the first I'm going to set the current health so the current health of the character is going to be what we have here I'm gonna put that on top oops and then next we're going to set the max health and set them out like so and basically just opens up gonna move that a little bit here so it's a little bit presentable cool so compile save one thing I want to do going back here is I want to make this instance editable and give this some default value so for the current health I want to say 120 for the max self make that's interested editable and make that say 200. so when I compile save go back play you would see my players now showing some stats cool so let's go back and do a comment here say accept current health now we want to create some damage so with the spear selected the Collision sphere selected here um right click and say add begin overlap so what we want to do here is when the third person character is the one overlapping so we want to cast BP to third person character we want to apply some damage so right click and select apply damage So currently you have this option so the damage actor would be the self so it'd be the current character that we have and the base damage at this moment is a float so we could hard code some damage here so I could say 0.5 would be 50. and and we also want to play a sound when we overlap play sound 2D so we know we're overlapping and this should be fine so compile save go here I'm gonna overlap nothing's happening at the moment because the event is not being received so here just gonna comment this and say apply damage and here right click and say event any damage so this is the part where any damage received by that actor would be um we could write some logic so here currently the damage is a float right so but the way we compute it is through integer so we want to trans make the damage the float into an integer so we could compute it so first we want to multiply the damage Say by 100 and then we're going to grab the current health and we're going to minus what the damage is and then we're going to set back the current health to what it is cool and then next we're gonna I have a reference to the wbp help and from here we're going to call the update Health custom event that we just created and then we're going to hook up the health and then set the target like so so now when I go back compile it was a duck from the player but right now nothing's happening it goes into the negative area so let's fix that so what want to happen is if it goes to negative we want it to spawn and then kill the actor so let's grab the current health let's say greater or less than oops less than or equal to zero if it's true we want to create a branch cool so if it's less than zero um what I did was I spawned animator I say attach and then the it was the capsule component where it was attached to and and then the emitter that I selected was actually I I think it was fire yep the fire I was playing some animation so yeah so let's say play animation mesh and from here select the goblin die nope not Goblin Goblin die I also put some delay let's put some delay say for two seconds after it's in two seconds only after it's been two seconds we wanna destroy the actor well save um overlap overlap overlap it didn't go to zero it went to -30 and then died so what we could do there is clamp some settings in here so under the current health here I'm going to attach this I'm gonna go under here Max health get I'm going to say clamp to touch that so the Max goes to the max here the value goes there and the return value goes here so that will make sure that the minimum value is zero it won't go over zero so let me put that up up there and play it again and boom and fire boom type now the other thing that I did was I created a child class from here so on the blueprint right click and select great child and from that great job open it up and basically I could change the uh the scaldal information here so I select the dice uh I think it was red that's that's fine um and then going back to um the variables here um under this gear here you could show click on show inherited variables and then from there I could set a different variable for this player so I could say 300 then 50 and I could drop them here rotate this guy a little bit so it's a little bit and I could also do the same thing if I have those values here because I have that instance editable I could also change it like so so when I do this now I have two characters boom dead boom boom cool cool so that's the video hope you learned something new uh thumbs up thumbs down please subscribe to my YouTube channel cheers bye for now
Info
Channel: Just Another Dang How To Channel
Views: 6,968
Rating: undefined out of 5
Keywords:
Id: uI5ps5DbFgI
Channel Id: undefined
Length: 17min 25sec (1045 seconds)
Published: Tue Jul 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.