How To Damage And Heal The Player In Unreal Engine 5 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another unreal engine 5 tutorial in today's video what we're going to be going over is creating a basic damaging and healing system for your player within your game so i'm doing this based on a previous video which i've created in which we did if i press play you can see we've now got a health bar in the bottom left hand corner which does actually work according to our health as well so in that video i asked if you wanted to see a damaging and healing system and a lot of people said that they would love to see that so that's what i'm creating today so you don't need to follow that video if you haven't seen already however if you do want to follow it it would be great because then again you will get the health bar which you see on the screen now but without further ado let me actually show you what we're going to be going over and creating today so if i were to press one i can heal and you can hear we get a sound effect as well and that's going to have different sound effects so we'll play a random one i've only got two different ones but you can add as many as you like and to damage the player all i'm going to do is walk into a box here and it's going to be the red one and that will damage the player once again with random sound effects i can damage and heal like so and if i go all the way down to zero health the player will die now i'll explain what i mean by that in a second no so the player died you heard a very strange sound effect there but don't worry about that i can still move about obviously nothing visual actually happened however i can no longer take damage and i can no longer heal so it does actually work in the sense of the players now dead they can't do anything however they can obviously still move about and do everything else like that that is because in the next video i'm gonna be going over a proper death system so that would be death and respawning which i'll go over in the next video but as you can see here the damaging healing system stops working once player dies because obviously you don't want them to keep taking damage and you don't want them to heal so again i'll be going over that in the next video so this is what we're going over and creating today so without further ado let me delete this code and i'll show you how i've done it so the first thing we want to do is we want to open up our character blueprint as that is where we're going to be doing the majority of this code so i'm going to hit control space to open up my content browser go to third person blueprints bp third person character however this can be in the third person first person or whatever you've named it once we're in here what you can see is i've already got this base code here for creating the health widget and putting it on screen we're going to ignore that because again we've already done that so what we're going to start off with first is damaging the player so what we're going to do is find somebody space right click and get event any damage as we're going to be using the engine's built-in apply damaged nodes after this we're going to hit the plus variable naming this one is dead question mark and leaving that as a boolean compiling it with a default value of false because we obviously don't want to be doing this code if the player is dead so first things first we're going to hold ctrl drag in that boolean to get it out of this we're going to get a not boolean not not equal sorry a not boolean that way it's going to inverse it so if is dead is true it will be a not which means this will be false reason why doing that is because we're gonna then hold down b and left click to get a branch connecting that into the event any damage and the not being the condition so if the player is not dead we're going to come off if true meaning we will do this code i'm just doing that because it just looks a little bit nicer coming off of true instead of false that's just personal preference but basically if the player is not dead we will do the code to damage them now actually damaging them we obviously want to set the health so we're going to hold down alt and drag in the health to set off of true there but what are we going to set the health to well that's very simple what we can do is hold ctrl and drag to get the health and we want to just do health minus the damage that is going to be in this event any damage node so drag out of the health and get a subtract node with the bottom value being the damage from the event any damage and the top value being the health which we just got it from and then this subtracts the value of it wants to go into a clamp that clamp just being a float like so i'm just going to move these down a little bit like this now the minimum value for this wants to be zero so the health will not go below zero and the maximum value doesn't matter too much but we're going to put it as our max health again the main one is the minimum being zero so the health is not going to go below the value of zero and this return value here is then going to go into our set health so that is what the value is going to be for our new health it will be our current health minus the damage making sure it doesn't go below zero and that's our new health and now if you followed what i did in the previous video you might need to do this as well so the way i'm updating my health on the widget is i'm actually setting it in the widget so if i get my reference to my widget here drag out this and do set health like so i'm just going to set that to the health which we just set up here now again if you've not got it set up that way don't worry if you've just got it set up as a normal binding so it's going to be using the health from the player character you don't need to worry about that but if you did what i did at the end of that previous video then you'll probably want to do this as well so again this part here is optional but after this we want to drag out of our set health either one is fine and get an equal equal operator like so and we're gonna leave the value as zero because we now want to just check to see if the player is dead if their health is equal to zero they're obviously dead so we're going to hold down b left click to get a branch with that being the condition like so so true means the player is dead false means the player isn't dead they've just taken damage so we'll do false first what i'm going to do is play a sound 2d now you can do ad location if you want but i think 2d sounds better for this kind of system it's just down to personal preference really but if you do do at location make sure you put the location as the player so just do get back to location for the sound of this i'm going to use my damage cue i'll go over setting up in a second at a moment's time sorry i've not done that just yet i'll go over that in a minute and we don't need to do anything else after this off of true what we're going to do is again play sound 2d or out location whichever one you are using and the sound for this is going to be just my death sound i've not done a key for that one as i've only got one sound effect so you choose which one you want to do then we obviously just want to set the boolean of is dead making sure we are setting it to true so ticking it like so and again in the next video i'm going to go over doing some proper death code here so we do ragdoll or normal animation and respawning anything that you want and we will compile and save that like so and so now we've got our damage system set up so i'm gonna select that hits it to comment it naming it damage system like so that makes sense for me and now i'm gonna very quickly go over the audio which i've just created so i'll compile and save minimize this and just find the sound effect which i've got so what i've done is i've imported all the sound effects i want so that's damage one damage two healing one and healing two those are all the sound effects which i have then you can right click go to sounds create a sound cue giving it the name of which you want so for example damage queue open that up straight away drag in all your sound effects which you want in there and just put them into a random node this means it will then just play a random sound effect now since i only have two in here it's a 50 50 chance so it is just basically going to be going between these two like so so see the more sounds you have the less repetitive it will be and the better it will sound but again just as this is for the purpose of the tutorial i just did two just to show you it working with random and multiple sound effects so we'll close that and just do that for all the sounds which you have and so now we're going to work on the healing system and so now we're going to minimize this again and what we're going to do is actually now damage the player so this is receiving damage but we need to actually also apply damage so minimize this and just go into the blueprint which you have for damaging the player so this could be an enemy ai or anything you want again for me i'm just doing it based upon a cube here so open up the actor which you have again for me this is just a cube with a box collision around it just so the player can actually overlap it then we're going to go to the event graph and this is where you want to decide how it's going to damage the player so if it's an ai you might want it to attack the player or if you're just doing a cube like me you might want it to be when the player overlaps it if it's overlapped we're going to right click the box add event and add on component to begin overlap and this is really the only difference we're going to have for me it's an overlap for you it might be something else obviously i can't cover absolutely everything because there's endless possibilities but do what you want to damage the player then to actually apply that damage what you want to do is get an apply damage node with a damaged actor it needs to be a reference to the player so you can decide how you want to do that for me that is going to be the other actor of the begin overlap as it is going to damage the actor which has overlapped the box collision so for me that's obviously going to be the player and the reason why i'm doing this instead of just a cast to the player is because let's say i have other ai in the level if they overlap the box i want them to be damaged instead of the player so i hope that makes sense and the base damage here i'm just going to set as 15. you can give this a random value if you want so you can just do random float like so or or if we were to do random float in range we can let's say do between 10 and 15. now what you might want to do is a random integer and then convert that to float because at the moment this can be 10 10.1 10.2 10.3 all the way up to 15 whereas if it's a random integer it will be 10 11 12 13 14 15. so do whichever one you want for me a random float will be fine what you can also do is do the damage causer so we'll drag out this and get a reference to self that essentially just gives more information to the player to say that it's this blueprint which damaged the player so if we go here you can get the damage causer there you don't need to worry about that too much if you don't want to set that up i don't personally use that a whole lot but you can basically just see what damaged the player it's just nice and easy to do and this is what we need to do in here so this is now actually going to damage the player because it's going to apply damage here which will then cause the event any damage to fire off which will then actually do all of this code here and obviously in here you can do a sound effect as well if you wanted so if this was to make a sound effect when it damages something just place that in here afterwards as well and so now we're going to close this and now we're going to set up the healing system so that's also nice and simple once again we're going to go back to the character blueprint and set this up and mine might be a little different to yours because you're going to want to make sure you just do this for how you want the player to heal so this might be by drinking something by walking into something or anything you want for me i'm just it going off of pressing the one keyboard event because that's nice and simple for me for the purpose of this tutorial and it's very easy to then add on to later on so what i might do as well actually is do this in a custom event so i can call it from multiple different places so in the character blueprint we're going to right click add a custom event naming this heel player and i'm going to add an input on here of heal value for example giving this a float variable and we're going to compile and save that straight away out of here what we're going to do is hold down b left click to get a branch because again we want to make sure that the player isn't dead they need to be alive and we also want to make sure they haven't got a maximum amount of health because if they've got the max health we don't want them to keep on healing so very simply we can get the billion of is dead like so shrek out and get a not boolean once again so if the player is not dead and then we want to drag out this and instead of going straight into the branch we want to get an and boolean because we want to make sure that this and something else is true and like i said earlier that something else is making sure that the health isn't full so we're going to drag in a reference to the health out to this get a not equal to which is an exclamation mark equal and this is going to go into the end and the bottom value of this is going to be our max health variable which we created in the previous episode as well so if the player is not dead and their health is not equal to their max health connect that to the condition we can then heal so now let's do the healing code once again we want to hold down alt drag in our health to set the health off of true of the branch but again what is going to be our new health value well if we drag in a reference to health here out of this get an edition node or an add and we're going to add our heel value from our custom event so i might just move those like that instead this is again going to go into a clamp float similarly to how we did it for the damage system minimum of zero maximum this is the important one of our max health so it's not going to go above our max health so you might be wondering why we did this as well because even if the player continues healing once at full health it won't go above their maximum health but it will also play the sound effect which we obviously don't want and let's say you're using something so you're drinking an energy drink for example to heal with a player it will continue consuming those drinks even though it's not healing the player which you obviously don't want to do because that would then be a waste so that's why we're doing both of these here and this return value is going to go into the set health like so with that now being our new health and again i need to set this in my health widget just to make sure that it is updating on screen as well for the player to be able to see like so then after this all i'm going to do is play sound at 2d to once again play a sound effect to also signify to the player that they have healed like so i'm going to select this hit c to comment it naming this healing system and i'm going to compile and save that and as i said earlier i'm going to get the one keyboard event and this is going to be how i heal the player and i'm going to give it a heal value of 10 so we compile and save that and you can call this custom event from anywhere you want so it can be an external blueprint you just cast a player core function heal player or it can be when you drink something when you eat something whatever you want however you want to set it up just call this function here so now we can minimize this and hit play to test out if i had to press one we get a sound effect our health has gone up i can continue doing that if we reach our max health i'm still pressing one nothing's happening if i were to walk into this cube we're going to take damage with a sound effect and our health is going down and then we can once again heal like so and if it goes all the way down to zero health we should then end up with the sound effects of dying like so and we can no longer take damage and we can no longer heal so that then works perfectly for us so i think that'll be it for this video as we've done everything we've wanted to do we've set up a system which we can damage the player and we can heal the player and if we also take enough damage the player is going to die which again we are going to advance upon in the next video in the next episode so thanks so much for watching this video i hope you enjoyed it and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see you in the next one [Music] you
Info
Channel: Matt Aspland
Views: 55,935
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, ue5, unreal engine 5, unreal engine 5 tutorial, ue5 tutorial, damage, system, function, node, apply, apply damage, hit, attack, shoot, fps, any damage, ue5 apply damage, fire, enemy, how to use, deal, health, take, dead, die, heal, sfx, how to damage, decrease, ue5 decrease health, death, take damage, deal damage, ue5 health bar, health bar, player, show health, max health, increase
Id: 5Xdnx3wPNLo
Channel Id: undefined
Length: 15min 43sec (943 seconds)
Published: Wed May 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.