How To Build a Health and Damage System in Unreal Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] here welcome back to the channel where I make how-to videos from subjects like mac tips back-end Swift Salesforce and game development in this version of the health system the health system appears in the top left of the screen as you can see here we'll look into several functionalities of the health system like when we pick up an item it would increase the health system we'll also look into decreasing the player's health so if I jump into this Podium there's a volume here that decreases the health and when the player's health is depleted to zero it would destroy the actor and this menu will come up saying game over and then try again and when we hit try again it would respawn the character back to its original state let's jump into it to start off I'm using a third person template with the starter pack on so go ahead and create that and then jump into your content drawer so under your content drawer here um just for organization I'm going to create a new folder I'm going to call it Main gonna create two folders here first is called blueprints and the other one is called UI so let's first build the help menu that we saw in there so right click here and select user interface and select widget blueprint so that widget blueprint there and let's name this we blueprint health double click to open so under the palette here uh something for canvas so this is the first thing that you would add is like a holder for the items that you want to add next let's um create a horizontal box so this allows you to add items side by side so adjust the X to 250 let's make that a bit wide and then position drop it down let's try 10 and position 10 so that should drop it a little bit from the index from this anchor so let's add a text here uh put it under there um then select that and let's call this text health uh even the text block and change the font to 16. and then select progress bar from the palette drag and drop it here you would see it would flow next there let's call this bar and I want it to fill uh put a padding of two and change the color probably to something around green so you can see it that's how it looks like um let's do this there you go make sure you select fill and that should expand there um but whether we're interested in populating right now is gray is the percent so here you would see that this value is actually a float when you move the float it would increase the progress bar so we'll figure out how to do that using binding later so for now um let's continue building this uh let's let's add another horizontal box drop it here and let's have a couple of text in here so you could click here and select Ctrl D to duplicate and I'm going to select all of these and change the font size to 16. so for the first one here I'm gonna call it current health and for the text here just gonna hard code something like 100 we're going to change that later and then here I'm gonna call this divider and then just change this to a forward slash and finally here we're gonna call this Max health and for now we'll just hard code the value 100. so if I look there that's how it looks like if I compile uh let's attach it here so we could reference it later now to add it to our level here we're actually going to use the third person blueprint that we have here so by default if you go under the event graph there should already be some existing codes here so what we're interested in is in this ad uh event begin play So at the end of event begin play right click here and say create a widget and from here select the blueprint that we just created and then we're gonna add it to the viewport and write it up and when I play now here you could see that the on the top left the help bar is now showing cool so let's go back to the third person and start creating a couple of variables so here we want to create um let's call this bar health and we want this to be a float and let's also create current health and but this time it's going to be an integer type and let's also create a Max health and same is an integer let's compile this and let's populate some data here so for the bar health because it's a float let's put in 0.5 and for the current health let's hard code that to 100 same thing for the max Health let's hard code that to 100. cool so compile and Save now go back to your um HUD the blueprint widget and go under the health bar here um so what we want to do is bind the percent to the health bar and the current tax and the max Health to the text from the BP third person character but so far we don't have a reference for the third person character from our health from our widget so what we can do is go under the event graph here and we could delete this two items here what we're interested in is the uh construct so once the widget is constructed we want to get the reference to the player so let's do a do once first so we only want to do this once and here we're gonna cast to the bpe third person character and let's also say get player character here and we turn that value there and then for here we're going to promote this to a variable um I'm gonna call this variable player so now that would create a reference to that third person character from here so basically that's all we need um let's put a comment here and say it create reference so compile go back to the designer and finally go to the health bar here and from the percent here under the blind you could now select the player and from the player here I should get the bar health which is what we need and then for the current health here because this is a text field um I could click here and then I could select current health and then for the max Health do the same for the text here select that player and then select Max health pile save so those should now be binded if I play here I should see the same thing but by health bar is now halfway populated cool all right so let's create some blueprints where we could increase the health of our our of our character here so first right click create a blueprint select an actor let's call this BP pickup over this uh let's create a Cube let's add a cube from the components here and then just resize this Cube we're going to make this look like cross there you go and then I could hit Ctrl D to duplicate it then rotate it like so and I could have some deck material here the green one the basic asset03 looks good and do the same for the other one and then finally I'm gonna add a sphere Collision here so let's say Spirit Collision it's a little bit small so we're gonna adjust the radius of that speculation so we're going to hit compile save basically that's it um if I go back to my content row over here and start adding some Spirits in here one more over here cool so this would allow us to um collect this items later but let's go back to that to our third person character here uh what I want to do next is create a function here which would allow us to collect the those items so under my third person character I'm gonna create a new function I'm gonna call this modify health and under modify Health here basically I want to create grab the current health and here as well I'm gonna have an input I'm going to call this health or damage so whatever we get but it's not a float it's going to be an integer whatever we get we're gonna add or subtract that from the current health so I'm just gonna do an add here and I'll explain later what this one so we're gonna add the current health once the current health is added we're gonna clamp it clamp meaning it shouldn't go higher or above the max out so the max cell would just be whatever value 100 and the minimum would be zero and then finally we're gonna set the current health whatever the value is returned from here cool and then one thing we want to do as well is um compute the bar hill so the bar health is a float but now we're dealing with integers here so one way to deal with this is to actually create a divide the current health by the max health and that should return a float so let's do that so let's first um I'm gonna change this to a float and same here at the max sales I'm going to change this into a float and basically I'm going to just divide this divide by that that and then I'm gonna set the bar health by quick unwrapping that so whatever the division is so if 100 by 100 is going to tell me one and that would be the part health cool and finally let's um return the node return node touch a bit save file and now back to our pickup for our pickup item here because we added a spear Collision um go under the event graph and I'm just gonna delete this and spirit Collision back again I'm gonna add a begin overlap so basically when it's the other actor it's where it's the BP player that's over overlapping the Collision box you want to get reference to that actor and then we're gonna call the modify Health that we just created we created an input parameter for it so for that we could past 25 as a Health input if it was negative it would be a damage input but because we added a positive one it should add to the current uh health value now after adding that I want to destroy the actor and I also want to play some sound play sound 2D and let's just play this one the camera shutter sound cool so when I compile save and then run this now when our third person overlaps there you would see that the value increase by 100 which is not what we want so it should just increase by 25 so let's go back here so the current health is 100 here I'm going to change this to 50. all right so that looks better so when I overlapped this this should just increase it by 25 25 and they shouldn't go over more than 100 so there you go actually that's how you increase the health I'm going to add one props in here one component I'm gonna just add a rotate because I want that yeah there you go and go back here play that should make them spin cool so that's how to add the health system now let's try adding um adding damage like basically calling the modify help but this time we're going to add a damage so back here I'm just gonna go on this Podium here and I'm gonna create a um trigger volume either a trigger volume trigger box um I'm going with the trigger volume here and then I'm just gonna size this up to be as big as the box that I have here cool so with this selected I'm now going to my level blueprint so under the section select level blueprint and if I right click and then select this and say Collision I can say add begin actor overlap because I've selected the trigger volume it knows that I'm on that trigger volume cool so here what I'm going to do is I'm also going to do the cast to BP actor cast to BP third person character and from here um I'm gonna call this modified health cool so when I modify the help instead of adding a positive number I'm going to put a negative one so I'm going to say minus 25. now from the demo that you saw I have some bit of effects going on so what I did was I spawned uh actor for glass and from there what I'm doing is I'm spawning the blueprints effect fire and then for the location um from the actor for this one you could get the capsule component as well I want the transform get World transform and wind it up and that should spawn the blue fire effect on that character and then I also want to attach it to the actor so here say attach to after component and for your Target it's going to be this one for the parent reference this and attach it to the capsule component and then it should snap to Target so let's test this out first go back here back here so when I overlap here you would see that my character starts smoking and then the health got decreased by 25. if I go out the Fire doesn't stop but if I go back to that trigger volume you see the health is now zero so few tricks that we could do is we could remove that detach that component after a few seconds and also check if the actor has zero Health that we would want to show a different UI or end the game cool so let's go back to our third person map here and then I'm just going to put a delay um say the layoff three seconds let's let's try two seconds and then once it's completed we want to detach the effect from the actor so say detach detach all right so I can't do that there what I can do is go back here and under this effects that we just created I'm gonna promote this to a variable so I'm just going to wire that up um let me link that link that and for the new variable here I'm going to call this effects and then going back here I'm going to drag the effects I'm going to say detach from actor cool that should detach it from the after after two seconds so let's try that out so go back here one two after two seconds that's gone cool so that's how you um apply the damage now let's create a UI where when the health reaches zero um it would I'll show the game over a status so under user interface here go on the widget blueprint select that and say w blueprint I'm just gonna say game over open that up so same thing as before first thing you want to work with is the canvas I'm gonna add a canvas there I'm gonna add a button I'm gonna drag the button somewhere here in the middle I probably want to Anchor this in the middle there and then reset this guys all right so for the button let's adjust some settings here the one that I have is on 150 by minus 50. and size is 300 by 100. cool and then I'm gonna have a text here drag it there in the middle um for the text I'm going to set this to 30 and then I'm gonna call this try again and then another text which I could drop somewhere here and this time this one is gonna say game over for the game over um I'm gonna anchor it as well in the sorry in the middle and then change some settings here so negative 150 by negative 150 and 100 by 30 and change the font to change to 30 and it's not pretty lined up so I'm just gonna move the Y EP that should work so save um so what I want to do is go here under my third person character then go back to the event begin play and basically I want to create a new custom event here so custom event and I'm going to say Kill player so under Kill player here I'm just gonna put a delay say of two seconds then I'm gonna destroy the actor once the actor is destroyed I wanna pause the game so set game pause and then I wanna create a widget widget there you go and this time it's going to be the game over widget and we're gonna add it to the viewport and to viewport cool and I'm just gonna comment this and say Kill player cool so now we need a way to call this Kill player when the health has reached zero so one way to do that is on the modify begin Health that we've created on the function so instead of returning the node immediately we're going to create a branch so here I'm going to say branch and let me detach that first so the condition we want to check is if the bar health is equal to zero so we'll say equals so if it's equal to zero that means our player is dead so we want to call the Kill player custom event that we just created if it's not just go ahead do the normal return node cool so let's try that out all right so we got an error let me go back to my Kill player here I think my error is the add to viewport here I didn't tag it properly cool so compile save so that works now let's try go here 25 Zero game pause I can't move it anymore the problem is I can't click the try again button so let's now add functionality so we could click on that try again button once the game is pause so go back to the game over section here and go under the event graph or the graph section and here we want to delete this um where we can play an event tick we're just interested in the event construct so what we want to do here is get the player controller and we want to set Mouse cursor that mouse cursor said mouse cursor there you go so set the mouse cursor we want it to be visible so tick that and we want it also on the set input mode UI only so I'm going to hook that up to the player controller here and that should be it um I'm gonna compile save so once that widget is added to the screen it we should now see the option for using the show mouse cursor and then we could click and then um uh restart the game but going back to the designer here if I click on this button here we need to do the on click right so on click it would create this function here on click of that button you want to get the current level once we get the current level we want to open level by name and just attach that and it will do its own conversion compile save now let's give that a go so hopefully this should work characters on fire after two seconds is gone go back to the pool gym it's game over now I have the cursor showing and I could click on try again but the problem this time is it respawn on the same area but I don't have control of the character because it's still on the mode UI we should now go back to the third person character here go to event beginplay and at the end of event begin play I could go back to my game over node here and I could copy this uh two nodes and paste it here so basically once we event begin play happens we want the show mouse cursor to be removed and then we want it back to the input kmode only and we're gonna hook this up like so and Save and let's try it again so click yep increases out decreases the health go back and then for the very last time it's now zero boom so character is destroyed I could click on try again and there you go now I have control again it's resetted the uh game for me cool so hope you learned something from this video thumbs up thumbs down uh please subscribe to my YouTube channel
Info
Channel: Just Another Dang How To Channel
Views: 926
Rating: undefined out of 5
Keywords:
Id: 6hoXr1Ts6oM
Channel Id: undefined
Length: 31min 15sec (1875 seconds)
Published: Tue Jul 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.