Why?
The crouch. One of humanity’s most basic movements. Humans have been crouching since they figured out that going under things doesn’t always require us to crawl. Sometimes in games we need to duck under objects or sneak past enemies so this mechanic can be very versatile and we can build many different game mechanics into it as an example some of these could be to improve accuracy when shooting / Snipping, to reduce the characters detection rate when sneaking some games have even used crouching jump combination to reach higher platforms. All in all it can be a very useful thing to have in your game.
Resources
Provided Crouching Animations converted from Mixamo
How?
Action map and blueprint
Since 5.3.1 released we do action mapping a little bit different, no long are we going into your project settings and mapping from there. Now we are adding an Input action and adding that to a IMC (you can have multiple of these)
Now we are going to create a new input. Navigate to Content>Thirdperson>input>Actions. Right click and go to input and select input action and name it IA_Crouch. Once created open it and leave all the options as default and click save and close it.
Now we need to add it to our input mapping context, should be in the previous folder. Its call IMC_Default, open this up so we can add our mapping.
Use the plus button and from the drop down select our IA we just created and bind it to the Left CTRL key and make sure you save.
For our next step we are going to open our character Blue print (ThirdpersonBP) and setup the blue print for crouching.
Left click and search and add the action map we just created IA_Crouch and click on its expand arrow. Drag from the Started pin and search for and add the crouch function. From the completed pin add Un Crouched.
Select the Character Movement in the components tab on the top left and search for crouch.
Here we are going to click on the option at the bottom called can crouch.
We can also change our crouch speed as well as our height. The height options changes our collision capsule so we can fit under objects or landscaping you can change this if you wish. This wont make your actor animate into a crouch yet it will just reduce the size of the collision capsule. If you decide to test it at this point all you will see is the camera move. (You can also make the collision capsule Visible to test also)
Another thing that you can do at this point is that If you feel the camera movement isn’t smooth enough you are able to enable camera lag. What this will do is smooth-en the camera movement when you toggle the crouch. In order to do this select camera boom in the components tab then search for lag in the details tab and enable camera lag
Animation configuration.
Next we are going to setup the animation, we need to import the animations that are compressed in the CrouchAnim.zip file. You can click and drag or add them how you see fit. Create a folder under content call it Animation (Can add a subfolder if you wish) and add them here. You will be prompted with a FBX import options, target the SK_Mannequin skeleton and import all. Once done you should see all Five animations imported. (We are only going to use two for now, however having the others there will be good for future use)
Before we setup the blend space we need to modify the CrouchedWalking Animation to stop it repeating so open it up and on the left under asset Details we are going to enable two options EnableRootMotion and Force Root Lock
Now we are going to setup our animation blend space. To do so right click in your animations folder and goto Animations>Legacy>Blendspace 1D and select the sk_manequin and call it ABS_Crouch
Open up ABS_Crouch and Under the Asset details tab find the horizontal axis and change the name to Speed and set the Maximum Axis Value to the speed you set in the blueprint. Default is 300. Also change your grid divisions to 2 and select snap to grid.
Now on the right under asset browser find your crouch idle and drag it to the left side of the time line and then find your Crouchedwalking and drag it to the right side of the timeline.
If you hold Left CTRL and move your mouse over the time line the animation should play.
Save and Exit.
Configuring the AnimGraph in the character BP
In the content browser navigate to Content>Characters>manneqiuns>Animations and select the ABP_Manny. (this also works with custom characters.)
Find the Sequence node in the middle of the current screen and select add pin, next either get the movement component or copy and paste it from one of the existing one’s on the screen. Drag from the pin on the new movement node and add is crouching. Right click the return value and promote it to a variable and call it isCrouching? Now connect the Set to the new pin you created earlier.
Now goto the eventgraph tab in the top middle and when it is open access the AnimGraph this should be a tab in the middle top section of the screen and open up the locomotion state machine. You should see something like this
Create a new state machine and call it Crouched drag off the pin and create a new Cache pose and call it crouched.
Open the Crouched State machine and add a new state from the entry and call it crouched locomotion.
Open Crouched locomotion and add in our ABS_Crouch animation from the asset browser to the right and connect it to the result. Then from the speed pin drag it out and get ground speed.
Go back to the main AnimGraph page and open up Main States State machine. Add a new state and call it Crouched Locomotion and connect it to locomotion and back (this can be tricky at first, try dragging from the border)
Click on the locomotion to crouched locomotion. Add the variable isCrouching? And connected it to the result.
Now do the same for Crouched locomotion to Locomotion, add the variable isCrouching? From the pin drag it out and add a NOT bool and connect it all to the result.
Open up crouched locomotion now by double clicking now add our cached pose (Crouched) to the result
Compile and save.
Conclusion
You have now created a crouch input action and designed an animation blend space to facilitate crouching. This means you can now duck under low objects and hide behind cover like a true stealth master. Well done!
This versatile mechanic opens up a world of possibilities, from enhancing shooting accuracy to reducing detection rates. How you choose to implement and balance these pros and cons is up to you. Happy crouching!