I’ve just learnt how to let a player character crouch. It’s a little complicated so I thought I’d take some notes in the intricacies. All credit for this goes to Mathew Wadstein and his excellent video tutorial. I’ll explain how to implement the whole process using an interface call, and I’ll show how we can make the transition from standing to crouched smooth rather than jumpy.
We’ll need the following items for this to work:
- a Player Character (I’m using a First Person character)
- a Player Controller
- an interface
Our interface only needs a single function. I’ll call mine Toggle Crouch. We need a boolean input called isCrouched as well to communicate an already crouched state.
The Player Character
Let’s add the interface we’re using to our Player Character. The crouch-ability is part of the Character Movement component, which in turn is part of the Character object. It has two functions we can call: Crouch and Un Crouch. Let’s wire them up via a Branch Node, checking the incoming Is Crouched boolean (we’ll set this in the next step).
The crouch/uncrouch function will set our character’s capsule component’s crouched half height (yes… quite a mouthful). In other words, when we crouch, the collision capsule around our character will become smaller. Here’s where the default is set (with Character Movement selected in the viewport):
Our default capsule half height is 88, so crouching will set this to 40 (or whatever value we set here), while un crouching will set this back to 88. These values will become more important when we setup the smooth crouch later.
The Player Controller
Next we’ll jump into the Player Controller. We need to setup an input so the player can initiate the crouch. I’ll call mine Crouch and wire it up so that it makes the interface call. I’m using the C key, but anything will work (that’s done under Edit – Project Settings – Input).
I’ll use a Flip Flop node to detect the key press and hook up both white wires to the Toogle Crouch call on our Player Character (if this doesn’t show, complete the previous step first and compile). We’ll get the controlled pawn as target input and reference. The Flip Flop will remember its previous state, so we can wire the Is A output up to the Is Crouched input of the Toggle Crouch.
With all of this in place, our character will now crouch when we hit the C key.
Smooth Crouching (in principle)
Notice how the crouch height just switches between regular and low. We can make this look a little neater by animating the crouch height value using a Timeline and a Lerp Node. The solution is to animate the capsule component’s half height, as (presumably) the crouch/uncrouch function does. Here’s what that would look like:
Here’s how this works: Our Timeline gives out a value between 0 and 1 over the course of a short duration (0.3 seconds in my case). It’s output is wired to the Alpha Input of the Lerp Node, which does the actual value interpolation (between 44 and 80 in our case). This continuously updating value sets our half height. Timeline and Lerp Node is a killer combination!
While this works, it means that some of the other values (like crouched walk speed) are not set. There’s also an issue with the first crouch, but in principle this gives us a smooth up/down transition. I’m confident you can figure out the rest 😉