How To Punch An AI With Reaction Animations - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another engine 4 tutorial in today's video i'm going to be going over how to punch an ai so i've done this previously however i'm just improving upon it and making it better i am starting from scratch so you don't need to watch that video again i'm just making this one a little bit better a bit more efficient so for example we're going to actually react to more of the hands rather than any part of our body when we hit them and the ai will have a reaction hit depending on which way they're hit so if we punch them from the right they'll react to the right and vice versa of the left as well so the best way to show you is for me to just get in hit play and show you so i go over here i'm going to punch actually let me turn off the line trace there sorry that was for testing so if i go back in and i punch them you can see that if we actually go close enough to hit them we're going to hit them there so we hit them from the left they're right to the left and right like so so this works perfectly like this and again it's very easy to customize so you may see there that it's reacting to the wrong hand or anything like that that's just because when i've quickly set up the different animations but i'll show you how to do with your own animations and how to get them working perfectly for different ones and it's very simple we just need to rename the animations so i may have just given some of them the wrong name again we just rename the animations to get them to what we want so they have the correct reaction so without further ado let me delete this code and i'll show you how it's done so the first thing we want to do is we want to open up our character blueprint and this is so we can actually set up punching and our punching animations so what we're going to do is open that up so for me that's content third person bp blueprints direct passing character and once we're in here we also want to create an action mapping so we're going to go to edit project settings once it loads we're going to go down to input on the left down here and create an action mapping so let me actually delete that one there what we're going to do is just hit the plus action mappings here name it whatever we want so i'm going to name mine punch i'm going to change it from none to be the left click or left mouse button sorry you can give it absolutely whatever button you like it's just when we press this button it's going to fire off the punch action mapping which means we can fire up the code we want when we're punching so i'm going to close that like so and then back in our event graph and our cache blueprint we can right click and search what we just named it so i named mine punch you see we have action events punch there so like i say when we press the right mouse button it's going to fire off this code which is what we want for punching so i'm going to hold down b left click to get a branch connecting that to the pressed because we want to make sure that we're only punching when we want to and i don't want to be able to punch when we're falling through the air or when we're already punching so to do that i'm going to get the character moving from the top left up here and get is falling or just is falling sorry so it is falling there and then at the return of that we're going to get an and boolean and the other end of that we're going to right click promote a variable naming it is punching leaving the default value as false like so and then out of the end we're going to get a not boolean so if we are not falling and we're not punching and then that goes into the branch there now that i think about it the knot doesn't want to come afterwards it wants to go before because what would happen with that is what a knot does is it just inverses this essentially so if we're not falling but we are punching this and will be false because both of them need to be true so if that's false the knot will make it true meaning we can fire off the code but i said we're already punching so we don't want that to be true so we're going to get rid of that not and instead come out of is falling and get a not boolean connecting that into a and come out of ears punching and also getting not boolean connecting that into the second part of the end the end goes back into the branch like so so now if we are not falling and we're not punching we can fire off the code so you can just put them straight to the end and then come out false to the branch instead but i just like coming out true so then out of truth of this branch we want to set is punching to true because we are now punching and after this we want to play our animation so what i'm going to do is also create another variable and naming it left hand or is left hand question mark or in right hand so this is essentially to see which hand we're currently punching with and i'm gonna set that after is punching as well leaving it as it is now we're gonna come back to in a second out of this we're going to get a play anim montage like so the anime montage we're going to right click promote it to a variable and i'm going to name this punching anim array like so or anything like that and then up in the top right here we're gonna select the variable icon up there and change it to an array which is a three by three grid there hit change variable type and close it like so so now we have an array and just disconnect it because we only right click permitted variable just to make it a little bit easier for us so we're going to come out of this and get a copy and that will go into the other montage there so if we compile this we can put our animations into this animation montage array here so this helps us get a random animation when we're punching and to make it random we're going to come out the index of the get and get a random integer in range minimum is zero maximum as the amount of animations we have so let's create those animations now so i'll move this over a bit move this back as well so like i said let's create them so we'll minimize this go to where we have our animation saved which for me is punch updated here so i have all of these animations i'll leave a link to these in description down below but i just got these from mix mo and them to the ue4 mannequin and as you can see i have them named correctly so i have cross punch right anim left impact left punch left punching right elbow because all of these have either left or right in the name of them because that way the code will know whether they are left or right so you want to make sure you've done that as well so if i open right hook punch for example you see we're punching with the right hand like so because i want the damage to be dealt by the right hand so i'm going to close that and then i'm going to select all of these animations right click on them create create and a montage and now we have an animation montage for all of these animations like so so we're going to go back into our character blueprint here select the animation array and we can add in as many as we want here in the array element so actually let's see how many we have so i have one two three four five six seven and eight i believe so let me add those in so we're going to add in eight array elements so we've got zero one two three four five six seven and now because we started at zero this is eight so let's add in all of these so i'm going to minimize that select them in here and press the arrow there to easily put them in make sure it's the animation montage not the animation so the cross punch i think i may have counted left impact so we don't need that left punch left punching two right elbow right hook and again you can use as many as you want you don't need to use all of these and it doesn't need to be random either you can just use the one right punching and right punching three so yeah i don't need that bottom one so what i can do is just delete that like so and now we have our animations in here so now it's going to play a random animation between them so let's set the max as six as we have six different array elements in there so like i say this is going to play a random animation for us which is perfect but we also want to set whether it's the left hand or not which like i say we're doing via the titles that we gave them so we'll currently get a copy again we're going to get display name so it's going to get the name of them and now the return value of this we're going to get a contains under string there we're going to search in that display name with the substring of left so if it is the left hand or not which again we have in the name of them we don't need to take use case or search men because use case means it has to be uppercase or lowercase which we don't want and search mend means it's going to search from the end so i'm going to leave it as this with the return value going to set left hand there i'm going to move that out a bit like so so now we're going to play a random animation and the code knows whether it's the left or right hand dealing damage which is perfect so now after the play and the montage we're going to hold down s left click to get a sequence connecting that in there like so what we're going to do here is come out the return value of the play and montage and get a delay like so connecting that into then zero of the sequence now the completed of this we're going to set is punching to be false so once we've finished playing the animation so we've finished the punch we're gonna set his punch into false so we can then punch again out of then one we want to do some code for actually detecting whether we've hit the ai or not so let's do that now so what i'm going to do is just come underneath this right click and add a custom event i'm going to name this one punch trace as we're going to be doing traces so then out of this i'm going to hold down b let go to get a branch because we want to loop this but only while we're punching so i'm then going to put the is punching in the condition there like so out of true of this we're going to get a capsule trace for objects there the start we want to do on our hand so we're going to drag and drop a reference to our mesh here so get mesh out of that we're going to get socket location like so in socket name i'll do in a second but the retinal of this is going to go into the start and end with the capsule trace because i just want the capsule trace to only be on the left hand then in socket name we're actually going to come out of that and get a select like so option 0 being hand underscore l option 1 being hand underscore are because that is then our left and right hands so if you're not sure what they are for you because they do need to sew exactly the same you can open up your skeleton which you're using the animation on so for me that's going to be in content mannequin character mesh skeleton so the queue for mining skeleton you see if i select the right hand it's hand underscore r and the same on the left is hand underscore l so make sure you spell it exactly correctly like so i'm gonna close that back on the select we're going to change the wild card to be boolean so it's going to select whether it's the left or right hand dependent on a boolean value the index of this wants to be our boolean left hand we just made so actually i want to change this around because you see the left hand goes in there if it's false it needs to be hand r so let's change that to hand r if it's true it is behind l because if left hand is true we want to fire off the line trace so the damage is being dealt on the right hand or the left hand sorry so again using the names up here we're determining which hand we want to do the code on which is going to work perfectly for us back on the capsule trace the radius i'm going to have is 22 the half height is 22 as well object types i'm going to make array and in this i'm going to change it to pawn because we want to be overlapping a pawn which is another player or an ai or a character of any kind and everything else we can leave the same making sure that you do have ignore self ticked because otherwise it's going to try to punch itself which obviously we don't want to do after this we're going to hold down b and left click to get a branch connecting that into the execution and the condition into return value and out hit we're going to break hit result the branch is so that we're only firing off the code if we hit something and the break hit result so we can see what we hit so what we're going to do is open up the brake kit result come out of hit actor and we're going to cast to what ai we want to punch which for me is ai punch updated like so connect to that into the true like that out of as ai punch updated so out of your character we're going to fire off some more code which we need to make which we'll do in a second out with false of the branch we're going to hold down d left click to get delay connect to that there the duration is going to be 0.01 and now they're completed we're going to call event punch trace which is going to loop it until we're no longer punching so that will work perfectly for us and then we need to call this as well so it does fire off and that's again out of then one of the sequence we're going to call function punch trace there like so when we start punching we're going to fire off the trace and the trace is going to loop until we stop punching which works perfectly and again there's only one more thing we need to add on here which we'll do after we've created the code in the ai so let's compile save and open up our ai blueprint which for me is in my punching folder so punch updated mine's just ai punch updated there in here what i want to do is right click and add a custom event i'm going to name this one hit or on hit or punched anything like that and also i should mention if you don't have an ai you can very easily just duplicate the character blueprint i get one and i do have videos where i'm creating anyways as well if you'd like to go see that but i assume you already have one since you're watching this video out of this hit custom event that we just made here we're going to hold down o and left click to get it do once connecting that in there and that's just to prevent it from spamming because we don't want to fire this off every single second and every single millisecond if you want that the character is overlapping and punching the pawn we want to do it once so the player punches the pawn it reacts if it punches again it'll react again but not for the whole duration that the ai's hand is overlapping so out completed we're going to play sound at location if you have a sound effect that you want to add for this so i believe i do from the previous tutorial which is probably just named punch or impact impact q there the location we want to get actor location so it's going to play the sound effect at the location of the ai i'll leave a link in the description down below to the sound effects as well out of this we're going to play anim montage like so with the anime montage we're going to get a select node once again and we're going to change the wild card to be a boolean again and we want to access the left-hand boolean we made in the character blueprint so what we want to do is create a reference to our character blueprint so we're going to hold down p and left-click to get event begin play out of this we're going to cast to our character which for me is the third person character and the object is going to be get player character like so and then as third person character we want to right click promote a variable name in this one character reference and now we have a reference to our character which if we come at this we can access that left-hand boolean there we don't want to do it there what we want to do is come back down here drag and drop that character reference in here get character ref out of this we're going to get left hand that will go into the index there so now we're going to play an impact animation depending on whether it's the left or right hand so if it is the left hand so true we want to get our left impact and the montage we made earlier and false wants to be our right impact montage we made earlier as well and that's perfect now it's going to play the correct animation i also want to decrease the ai's health when i punch them so what i'm going to do is hold down d left click to get a delay connect the return value into there like so into the duration sorry so it's going to fire off after we finish punching them if you want to do it as soon as you punch them you don't need the delay here but you'll need to put it after we set the health so i'm going to create another variable naming this one health so hit the plus verb on the bottom left name it health the top right change it from a boolean to an integer or a float either one will do we're going to drag and drop health and set it there and then we're also going to get health minus an integer so integer minus an integer going into the health there and then we're going to create another variable and i mean this one damage keeping as an integer connecting that into the bottom minus there we'll compile and change the default values so if we select the health i'm going to have the default value as a hundred so the ai has 100 health and the damage i'm going to set to be 25. so every time we punch them we deal 25 damage now a good reason for putting it in an integer is because you can then change this later on via code if you wanted so one punch might do more damage and i do have videos on doing that as well setting up a better damage system like this which again i'll leave in the description down below and on screen now after this we want to see if the ai is dead or not so we're going to come out of health and get a less than or equal to so an integer is less than or equal to an integer leaving the bottom one is zero because obviously if they have less than or equal to zero health they're going to be dead now let's hold down b left click to get a branch but that's the condition the execution go into the set health because again we want to see if this is true or not out of false so if we're not dead we're gonna go into the reset if they do once there so that they can then be punched once again because obviously if they're not dead we want to be able to punch them again so we can kill them like so out of true if they are dead we want to play an animation to them dying so i'm going to come out true and once again play sound and location so again you can play a sound effect if you wanted to the sound i believe i called dying or death death cue there location is again going to be get actor location so it's wherever the ai is after this we're going to play anim montage using a similar method we used before of playing a random animation so i'm going to right click this promote to variable naming it def anim array disconnect it like that and then i'm going to change it to be an array off my top right like we did earlier change variable type close that compile and then we can add in our depth animations in here so let's hit a plus add element search for death or dying and i have three so i'm gonna die in one montage and add two more and then add in my other ones so dying two montage and just dying montage so now it has a 33 chance to play one of these random montages here which is perfect again it just keeps it a bit different it stops it being so repetitive of having the same animation each time out of this we're going to get a copy connecting the output into the montage there and again the index is going to be a random integer in range minimum is zero maximum that's two as we have three animations so we've got zero one and two and then we're gonna hold down d left click to get another delay connecting the execution return value in there like so and out of this we're gonna just simply get destroy actor so what it's gonna do is just simply get rid of this ai once they're dead so we're gonna compile save that go back to our character blueprint and as ai punched updated or whatever it is for you we're going to call function hit or punch to whatever you named it and now when we punch the ai it's going to fire off this code to deal damage to them so that should work perfectly we can compile save that now there's just two finishing touches we need to do so if we minimize this we need to open up our animation blueprint which for me is content mannequin animations third person and mbp and i have the same one for the character and the ai if you have separate ones open both of them up and make sure you have slot default slot in here like so as that allows us to use animation montages if you don't have it you can simply just come out the state machine here in the anim graph so select animegraph up there stay machine and get a slot default slot like so connecting it between the state machine and the output pose close that and the other thing i want to do is open up is open up the dying montages so for me that's going to be in content and then punch updated open up dying one montage dying to montage dying montage like so and what i want to do is just make it so that these don't end so at the end of them they're going to stand back up i don't want that to happen i just want them to load on the floor until they're destroyed so a simple way to do that is just untick enable auto blend out and that means they'll just stay in the final pose i'm gonna save that and do that for all of these this is a very simple little trick this one's very handy and gives us a nice little effect as well and now that should be everything done for us so if we drag and drop some ai in here which i already have so i hit play to test this out if i left click we get the punch there so that works perfectly so now we're punching the random animation each time if i go over here left click to punch we punched with our right hand they reacted with the right impact if we punch with the left that didn't hit but again i think what i've done is i've named them wrong if i punch the right there that worked so yeah let's check the names because i believe what it did was it ran the check on the right hand instead of the left purely because the names were probably wrong in here so again the simple way to do that is just make sure that you open them up and check right so that's the right hand it's got right in there so make sure that a you've got them on the correct ones and b you've spelt them correctly as well so i'm just going to go through double check and change these to see if i need to change them and i'll get back to showing you how it works so again we can punch the correct animation correct reaction random animations and random death montages as well afterwards and this works perfectly for us and again we're dealing damage to them again the reaction is perfect all of that good stuff there they denied and then the animation stayed there and they got destroyed afterwards so i think that'd be it for this video is we've done everything we wanted to do we've just modified the code a little bit to make it a bit more efficient work a bit better and look a bit better as well so we have different animations actual reactions depending on which side they got hit from like that and also it works a bit better because we're using line traces or capsule traces sorry instead of just a normal box collision overlap so that just makes it run more efficiently like so so thanks so much for watching i hope you enjoyed it 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 in the next one
Info
Channel: Matt Aspland
Views: 28,162
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, 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, part 2, improved, updated, linetrace, trace, loop, detect, capsule, line
Id: nk85XLLnlCo
Channel Id: undefined
Length: 21min 46sec (1306 seconds)
Published: Mon Mar 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.