Punching With Random Animations, Damage Dealt, Death And SFX - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another religion 4 tutorial in today's video i'm going to show you how to implement punching and fighting into your game in which you can then damage the ai and eventually kill them now in this one the ai won't be fighting back and they'll just be standing still however we can advance on this in the future if wanted so what we're going to create is something which looks a little like this so we can go up to the ai here left click to punch we have a random punch animation so we'll play a random one each time they then react with an animation and a sound effect and then if we hit them enough they will die with another sound effect and a death animation and we can change how much damage it takes so how many hits it will take to kill them so i'll show you this one again and then we'll get into it so as you can see this is a very basic very simple starter fighting one however this is very good to get into it and to just start off with it and then you can obviously advance upon it with better animations better sound effects and stuff like that so i'll show you how to do this now so what you're going to want to do first is to import all these animations and sound effects so i've already done that but i will leave a link in the description down below for you to import them you just drag drop them in and press import all so obviously we have these animations here and then the sound effects here as well and also once you import your animations make sure that you select all of your animations which will be these green ones here select them all right click and go create create and in montage and you'll get an animation montage like that which is what we're going to be using so make sure you do that for all of your animations and you can pick as many or as little as you like and then once you've got all that sorted what we're going to do is i'm also going to be using an ai which i made in a previous episode however the only thing that i'm really going to have in here is well nothing at the moment but i have this random rom code from before so to create an ai what you can do is just go to your third person character ctrl c ctrl v to duplicate and that's now an ai this will be your ai you can just drag and drop in and that will now act as an ai it won't move about it won't do anything else but you have another character in there like so it's like say i'm going to be using the one which i already have but that is how you would do that so we're not going to open that up for the moment we're going to open our character so for me that's third person so i'll go third person bp blueprints third person character but for you this could be first third or what if you've named it and then once we're in here what we're gonna do is this is where we're gonna create the code for us to be punching so what i'm gonna do is find some space and up in the top left i'm going to go to edit and project settings and once this is loaded we're going to go straight down to input down here and we're going to create an action mapping so if you just hit the plus there to get action mapping and i'm just going to call this one punch or attack or anything like that and i'm going to set this one to be left mouse button but you can pick this to be named and have whichever key you like so it could be left mouse button right mouse button middle or f on your keyboard anything like that or even right trigger so you can have it on console and stuff like that and then we can also add in key bindings here but essentially just create an action mapping and pick the buttons that you want then we'll close that and back in the event graph if we right click and search for that so i called mine punch so we can get that like so so basically if we ever press that button we're going to be punching so out of this if we hold down b left click and get a branch plug that into pressed and for the condition i'm just going to get is falling from the character movement like so the reason i'm doing that is because i'm going to be disabling the movement so if the player presses this while they're in the air the character will be just floating there which obviously we don't want so i'm going to do that like so and then i'm going to create a new variable and i'm going to call this is punching like so so it is punching question mark and then i'm going to just get another branch so hold b left click get the branch plug that into false so if we aren't falling we can then continue get is punching in there plug that into there like so is the condition so if we aren't falling and we're not already punching so come out fault again this is then where we're going to do the code so if we aren't already punching and we're not falling we can continue so out of this what i'm going to do is get a reference to the character movement here so drag and drop that in from the top left and just disable movement like so and plug them to the false of the branch there and the reason i'm doing this is so the character can't be moving while this animation is playing as that won't look great so when they're punching they're standing still and after this what i'm going to do is drag out this and get a play anim montage the target itself and the anime montage what i want to do is have this as it can be any random animation which i choose so to do that i'm going to create a new variable so hit plus variable here and i'm going to call this one punch anim montage array and as it's got array in the title we're obviously making this into an array so firstly we'll change it from a boolean to an anim montage so animontage object reference and select the little icon next to it and just change it to an array which is a little three by three grid there so you could also just right click on the return value here promote a variable and then change it to an array but i'm just doing it this way so then if we select that and drag and drop it in and get to that come out of there and get a copy and plug that into the ad in montage like so get an animation from this array but what it's going to do is always get the first one and we don't have any in here so if we compile this we can then put some in here now i currently have seven animations but you put in as many as you want and as many as you'd like to have so i'm gonna do is just get seven array elements here like so and if i just minimize that like that i can just do this a lot easier so if i then find these in here so that under new items for me i then have these animation montages so we can do is just select it press the little arrow there and we're going to be implementing all of these in here now i don't want the dying ones i just want the punching and so once you put in all of your animation montages like that you can maximize this again and now this array is full of our animations that we like to use so how do we then get a random one out of this if we come off this integer here from the get copy and we just get a random integer in range we can then do this so the minimum will be zero the maximum will be how many we have so we have seven so we're gonna put this as six as we start counting at zero so we go zero one two three four five six which is then obviously seven values so if we get a random integer between zero and six we can then get a random animation from this array here and that will then play it and then after this we're going to come out the return value here and get a delay meaning that this will just wait until the animation has finished playing so then once the animation is finished what we want to do is come out the character movement again and just set movement mode and set this as walking so that the character can then walk around again i'm just going to double click this to get some re-root nodes like so and then after this we just want to set is punching back to false again so that we can then do this again once we've finished that punch so we can compile and save that and that is all we need to do in the character blueprint code wise but we do need to do one extra thing and then we're done in here so if we go over to the viewport here what we're going to do is just add some box collisions on the player for hitboxes so what i'm going to do is just select the mesh and then use animation asset so it's then in a t-pose like that which will make this a lot easier and in add component what i'm going to do is get a box collision like so and i'm going to call this one right hand or hand right anything like that and we're just going to drag and drop that onto the mesh parent socket will be the right hand so hand r and then we'll just move this to where we want it and scale it down to be the correct size that we want just so it covers the hand and arm nicely so you can put this wherever you want scale this up to however you want but this is just where i want it and how big i want it to be so the reason we're doing this is because when the punch animation happens it will come out of the box collision meaning it won't have a proper collision on it so if we do this it's just a lot smoother and a lot better and also if you have different ones you can then choose which ones do more damage so if you have a kick the feet can do more damage than a punch or the other way around so i've done that and i want to do that again so just duplicate that but this time it's going to be for the left hand so then parent socket would be hand left and we can just keep it so since it's duplicated we'll do that but we'll just flip it on this head so that it's then covering the correct part of the hand and so that is now working perfectly and now if we select the mesh again we can go back to use animation blueprint and as you can see they're sticking with the hands like so but now that is all we need to do in the character blueprint so we can compile save and close that and everything else is going to be in the ai so we then find our ai blueprint here again and open it up like so again ignore this code this is just from a previous episode but i'm not going to be using it today so if we go down here and just find some empty space and get and right click get event begin overlap so event actor begin overlap like so with the other actor as our character so for me that's going to be cast to third person character but for you this could be first third well if you've named it and the object obviously being the other actor as third-person character what we're going to do is get is punching so this variable that we made earlier this boolean and then out of this what we're going to do is hold down b left click to get a branch and that's going to be the condition and just plug those in there like so so basically if we are punching then we can fire off this code if we're not so if they overlap and we're not punching nothing's going to happen out of the true want to do is play sound at location with the location as just get actor location like so and this is going to be the sound of the ai getting hit so we can actually create that now as well so if we minimize this go to the audio that we have i'll delete these cues and remake them so what i want to do is this is the damage so i'm going to right click here go to audio go to sound sorry and then sound cue i'm just going to call this impact queue like so i'm going to open it up straight away in here i'm gonna right click and get a modulator plug that to the output and then for the input of the modulator i'm going to get a random so i get random like so and this is where we're gonna decide to have a random sound which we have so i have three impact sounds here so i'm just going to drag and drop them in there like that drag them down and then plug those into the random so i'm going to add another input as i have three and then just plug all of those in and like i say this is just going to get a random sound effect each time so we have random animations random sounds this just makes it a lot less repetitive and a lot more dynamic so once that's done we can save and close that and put that into this play sound here so i called it impact cue like so and then after this what we want to do is once again play and in montage and this one is going to be target self again the anime montage this time is going to be impact now if you have more than one impact animation you can do the same thing we did in the character blueprint with getting a random one but for me i only have one so i'm going to leave it like that but obviously you know how to do that since we did it earlier you would just do this technique here and we're going to do that again later on if you'd like a recap so if we go back here and that's done like so we're going to do again is come out the return value and get delay so it is going to be waiting until this animation has finished and once it has finished we're going to reduce the ai's health so to do that we're going to create a new variable here so hit plus variable i'm going to call this one health and i'm going to set this to be an integer like so so then after completed of the delay i'm going to set health like so the input of this i'm going to get an integer minus an integer with the top value as being health and the bottom value as being damaged so i'm going to create another new variable and call this one damage leaving as an integer and plug that in the bottom there like so now you can set the damage to whatever you like and you can also set it in different places so when you punch you can set it to 50 or when you kick you can set it to 70. they're quite high but obviously you can set it to whatever you like so for me i'm going to set the health default value to 50 like so and the damage default value so make sure you compile to set that i'm going to set to 10. so this means i have to punch the ai five times in order to kill them so i think that's going to be good now the return value of this set health i'm going to get a less than or equal to so an integer is less than or equal to an integer and i'm going to leave that as zero so this then obviously means if the ai has died return value of that i'm going to go into a branch so that's obviously the condition like that to the set there so once we set the health if it's less than zero so the error is dead we're then going to do this code here so off of true we're going to do this false nothing so true we're going to play another sound at location with once again the location being get actor location and that's just because we want it to happen where the ai is not 10 meters away we want it to happen right here this sound we're going to do again is minimize this go back to our audio folder right click sounds sound cue this one is going to be def q like so open that up and we just want to import our death sound effect in there i only have one so that's just going to go straight in the input but the reason i'm doing this in a queue instead of just putting it straight in is as this has the location based in it and it works a lot better so we can save and close that and plug that in there like so so we have the death sound cue like so so make sure it is the cue like that after this we want to do it play another montage so play and in montage it's very similar to what we did earlier target once again being self and here is where we're going to do the random animation again so what i'm going to do is create another variable call this one dying montage array once again change it to an anim montage object reference and change it to be an array like so and like we did earlier and then if we compile we can set the default values which i have three different animations so we can get three different array elements there find our animations in our content browser exactly the same as we did earlier and just input these in there so i've got dying montage there and now we have all of those in there like so so we can maximize that and again that's the exact same way we did it last time so we can drag and drop get dye montage array get a copy plug the retar value of that into the anim montage and we're going to do the same thing with the random integer so we'll come out the integer here and just get a random integer in range with the minimum being 0 again but the maximum this time being 2 as we have three different montages so now it's going to play a random dying montage when the ai dies as well so if this what i want to do again is get another delay plug that in there but this time we're not going straight from the return value i'm going to go from the return value into a float minus a float plug that in there and we're going to minus 0.5 now you might want to change this for you but just for the animations that i have this works better otherwise the ai would start getting up when they're dead so we want to have it like this obviously mess about with this value to get it perfect for you but if you're using the animations that i'm using this will be what you want it as but you may also not need this if your character doesn't get up at the end of the dying montage so then out of the delay what we're going to do is just destroy actor meaning the ai is completely dead and so now this is all of the code done there is just one final step so if we have a look at this before we do that what's going to happen is when our character overlaps this and we are punching it's going to play the sound of the impact play a montage for the impact decrease the ais health if the health then goes below zero it's gonna die so it's then playing the dying sound effect and the montage and the montage will be random and also when we're punching we are going to be playing a random punching montage as well and then after the montage is finished in the death sorry is then going to also destroy the actor meaning they have completely died so we can compile save and close that and the final step is to go into our animation blueprint so i'm going to content mannequin animations third person nmbp and you're gonna want to do this for your character and for your ai but if they're using the same animation blueprint like mine are here then you can just do it in the one and what we're going to do is just go to this default animagraph here so you might be in the state machine like this so just go to the anim graph up at the top out of the state machine we're going to get a slot default slot and plug that in there and this just allows animation montages to play so if we compile and save that this should now work perfectly fine so if we hit play to test it if we left click you can see our character is punching and they're playing a random punching animation as well it's not always the same one so that's working if we go over to our ai we walk into it nothing happens if we left click to punch also nothing happens so let's see why that's happening so i imagine that is something in our character so if we open that up again i think i'll probably have the collision settings wrong on here so i've noticed what it is is in this code here we don't actually ever set is punching to true so we set it to false at the end but we forgot to set it to true so to do that we're just going to move all of this code out here drag and drop is punching there set it off there and just put it to true like so this just means that the ai will then actually take damage when we are punching because it will register as punching as well so now we compile save and test that again we can still punch perfectly it's going to be random animations like so and then if we go up to the ai and then we punch you can see it's now working perfectly like that so if we punch this many times so that's twice i said it needs to be five so then we get three four and five and they should now be dead as you can see like so so that worked perfectly so if i get more in just to test it out i can show you what this is gonna look like so i think our bit of this video is we've done everything we want to do we've set it up so that we can have a punching animation ourself and it'll be random each time and we can do this on any button press we like and we also have ai in which we can then punch as well they will react to it with an animation and a sound effect and if we punch them enough they will then also die with another sound effect and another animation like so so like i say this works perfectly everything we wanted to do we now have and like i say this is fully customizable by you in the ways that i showed you you can use your own animations your own sound effects and you can add more or less so thanks so much for watching i hope you enjoyed and i hope you're helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one you
Info
Channel: Matt Aspland
Views: 87,203
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, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, fight, fighting, punch, punching, deal, dealt, damage, health, kill, ai, kick, kicking, sfx, dynamic, random, anims, anim, animations, with, with animations, death, die, dying, to, free, easy, system, killing, ue4 punching, ue4 fighting, killing ai, damaging ai, hit, attack, chase, advanced, enemy, randomly selected
Id: 1jxElVA7g3c
Channel Id: undefined
Length: 18min 16sec (1096 seconds)
Published: Mon Nov 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.