How to properly crouch with third person animations - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i want to show you how to have our third person properly crouch i was struggling with getting this to work in one of my games and i was searching throughout the internet and i couldn't find anything helpful so i'm hoping i can make this video and help someone out so i want to start off here just with the brand new project as you can see i've done nothing in a third person template all i've done is add the animation starter pack and if you don't know how to do that it's super easy you just need to go to the unreal engine marketplace and then if you type in animation starter pack right here into the search bar it'll be the first thing that pops up so just click on it and then where it says add to project for me it'll say download for you so just hit download and whenever you go to your library it'll be in here somewhere and then you just hit add to project and select your project so it's super easy so let's get into it so the first thing we need to do is we need to set up some base stuff so i personally like showing collision when we're figuring this out because then we can see everything and then to test it i just want to move this block up a little bit we'll play make sure we can't just run under it to begin with so now we should be able to crouch under that when we're done so the very first thing you want to do is we don't have any buttons to be able to crouch so what how you do that is you go to edit project settings then go down to input you will see these action mappings and you can see right now we have jump and reset vr you'll hit this plus name it whatever you'd like i'm going to name it crouch and then assign whatever button you'd like i'm just going to make it left control and then if you want it to have multiple button clicks you can hit plus again and then you can have it have multiple buttons so we'll just do left and right control for now and that's it for that so now we have to code that in which is super easy the first thing we'll do is we will go to our third person blueprint double click on blueprints and we'll open our third person character and this is where all of the movement is handled right now so the first thing we want to do is just type in crouch and you'll see our action event crouch right here so what this does in unreal is it this input action this knows whether or not we're pressing right or left control which we have so then what off of pressed you want to drag out and hit crouch again and you'll see these two character there's crouch and uncrouch so for pressed we'll do crouch for released we'll do uncrouched and you'll see it's just right there there's a couple ways you can do this so how this works is if you hit left control and hold it you'll stay crouched and then right when you release it you'll be uncrouched you can either do that or if you type in flip flop which is right down here you can do it this way you can go from pressed and then a to crouch b to un crouch and then if you hold alt and click on this little line connection it'll take it off so what a flip-flop does is when you press it so when you press left control the first time you'll crouch then when you press it again you'll uncrouch so that's what i prefer so that's how i'm going to leave it for now and then to future proof this we want to add a variable which is on this left hand side just hit this little plus and we're going to name it is crouching and then over here we don't have to worry about anything it's good because it's already set as a bool so we're just going to drag it over here set it then we'll do control c control v to get another one and then connect them up so obviously if you're crouching you want to set as crouching to true so we hit that little check mark and then let's on top of that let's also hit make print just so we can test what we're doing so print string crouch and then we'll have pun crouch all right and compile so let's test it and as you can see i'll hit left control and you can see that the functions are working but we haven't connected it to anything so you can see crouching uncrouch in the top left corner but our guy isn't doing anything we still can't go underneath the block or anything and then there's no animations so we go to the animation starter pack and then we should see a crouch idle along with the crouch for forward so let's use this one and if you have your own animations in your own character this will work the same way i'm just starting with the very basics and what you want to do is just right click them and you'll see this retarget animation assets then duplicate them and retarget them this is how we want to get the animations to our character that we're using right now the first thing you want to do is hit this check mark show only compatible skeletons and you'll see our guy pops up just hit that hit retarget and it and there you go so now we have it saved on our content the next thing we want to do is go into this mannequin folder his animations and he has this third person animation blueprint already set up for us just makes that much easier so open that up and then you'll see we're in this jump loop to jump end rule we want to go back to default so this is our animation graph and we need to add crouch to it so off of idle slash run we're going to drag and then hit add state and we're going to name it crouch hit enter and as you can see right now this arrow it's only pointing it's only pointing up to crouch but we want to be able to go from crouch to idle to run also so how you do that is you just drag from crouch to idle to run then we want to double click crouch and if we make this a smaller screen and if we on back in the engine not in the code if we hit content we will see our two animations we duplicated all you have to do is grab your crouch idle and drag it in and then once it's dragged in you just want to connect them hit compile and then you'll see you get these two warnings and what these warnings are telling you is that there's no way to enter the transition the game doesn't know how you want to be able to crouch so we'll go back to default and then we'll see from idle to run we'll double click this then we need to make a variable we're going to name it is crouching again just so our naming convention is the same so is crouching we're going to get it and we're going to set it equal if you are crouching then you can enter enter the transition and make sure to hit that little check mark we'll compile it and you'll see that that warning goes away down here so now we'll go back to default and do the other one from crouch to idle we'll grab is crouching we'll get it and we'll type in not so if you are not crouching then you can enter enter the transition and we'll compile and you can see the warnings go away but if we test this you see nothing still works that's because we haven't determined what is crouching means in the animation blueprint so let's do that real quick first thing you want to do is go to this event graph right here and then this works out really easy for us so after this very last set speed we want to cast to the third person player cat so you type in cast third like a type third person character and then the object every time you cast a third person character you have to get a game object and that's just the player controller and then hit enter there we go now as this third person character we want to get is crouching that variable we made earlier or is crouched which is what i ended up naming it and then we want to grab our is crouching and set it and connect all the nodes right up and that's all we have to do oh just kidding that's because i got player controller you need to get player character there we go so make sure you grab the right is crouching then you'll compile it if we hit play we will see if i hit left control that i'm crouching but it doesn't affect my collision at all and now i'm just surfing everywhere so that's not really what we want either but we can crouch and uncrash for now so the next thing we want to do is make it to where we're not surfing everywhere so we will go back to our animation graph right here and then off of crouch we're going to make another state we're going to make name this crouch walking and then again like what we did with crouch now to run we want to have an arrow going both ways so we're going to double click crouch walking and then we want to grab this crouch forward walk and then just connect them up make sure to compile it and we get those two errors again so from crouch to crouch walking this one's a little more complicated because we have to add speed into the variable so we want to grab is crouching you just want to get it and then make sure it's equal and then we want to grab an and bull and then after that we want to grab speed get speed then off of speed we want to grab in greater than and equal to float let's connect the bull to the and and then connect that to the can enter transition and where this says 0 0 we want to type in 10 for the speed and that you can change this depending on what you prefer and how what your game needs and don't forget to hit this little check to make it true we'll compile it and we'll see it'll take us to the other one that's only warning left so now we'll do from crouch walking back to crouch we want to grab is crouching and then we want to make a knot pool off of it then we want again we want to get another and boolean off of it and you just connect that up for now and then we want to get speed have a less than or equal to float and connect that and for this i'm going to do five but like again you can depending on your game you can adjust those settings so we're going to compile it and now we will go back and test our game we can crouch and we can walk and we're not surfing anymore and then so that's good so the next thing we want to do is get the collision working because we still can't go under this even though it looks like we should be able to so the first thing we want to do is go back to our third person character blueprint that if you click on this character movement inherited if you type in crouch you can see this little can crouch you want to check that it's very important and then you can also see this crouched half height right here you can adjust this to make the capsule however big or small you want depending on your character size so we'll set it to 55 for now i think that's relatively close to what our crouching height is and then one last thing that i want to do just to show you is if we go to our capsule component and then this hidden in game we're going to check that off just so we can see our collision so now we hit play we can see it if we crouch there's our height and now we can go under this but if we hit crouch again we stand up inside and we don't want that and then once we get out of the block then it just pops back up that's really sloppy you don't want that so how we go back and fix that is we go back here and this is where the real magic code happens so off of b from this flip flop or off of released if you don't have it you want to make a capsule trace by channel node and then we want to grab our capsule component and just drag it in we want to get the world location then off of that return value we just want to hit plus on the keyboard and get a vector plus vector we'll drag it back a little farther and then grab this end pin and connect it into start and then off of the capsule component again we're going to get the world location off of that we want to get we want to get an up vector because that'll go above our head we want to connect that to a times so just shift and eight and this we're gonna do a vector times an int you can also do a vector times a float if you prefer that it doesn't matter and then we wanna connect the end of this into the plus and then the times just straight into the end and then this where this says zero i'm going to type in 200 for this test project and what that 200 is is that's 200 units above our head is what it checks which sounds like a lot but it's not as much as you think and then we're going to disconnect our uncrouch and just scooch it over for now from this out hit we want to get break hit results then we want to go into a branch so from the initial overlap so that's right when we hit the block above our head or something above our head type in branch and connect those up before we connect these let's print strings so we can see what we're doing connect the true to one string the false to another string and connect the false string to the uncrouched the true string to the crouch we will name the true cannot uncrouch and then the false will be can on crouch then after that we'll compile it we will hit play and now you can see we run up to the block hit control we can go under it i can hit control again and i won't uncrouch it says i cannot uncrouch we'll go again and then we can uncrouch but there's one issue with this code right now so if you go underneath and hit the crouch button once and then try to uncrouch it won't let you you have to hit it twice so we want to fix that so it's a little neater but we're almost there so to fix this it's super simple we just want to go back to into our code and then off of our a from flip flop or from pressed if you're not using it make a branch i'm going to actually just scooch all the stuff back it'll make it a little neater our condition is going to be is crouching so get it and then connect it right up we're going to have our true go into this so if you if we are already crouching we want to check that nothing is above our head and then our false if we're not crouching then we then we want to crouch so then we can compile it hit play and we can see that bug doesn't happen anymore so we'll hit it once and then we go back up and we can do that we can hit crouch as many times as we want and it'll work perfectly fine and that's how you do it i hope this helps someone i had a lot of trouble trying to get this to work in my game so i hope this helps a lot thank you for watching
Info
Channel: Sean Mercer
Views: 5,410
Rating: undefined out of 5
Keywords: Ue4, Unreal Engine 4, Crouch, Crouching, Tutorial, stay crouched
Id: XC1RpkqIo34
Channel Id: undefined
Length: 17min 33sec (1053 seconds)
Published: Sat Jan 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.