How to write a smooth crouching script in blueprint?

I work on the community game Trillek, and we have been unable to figure out how to get the player to crouch other than using the default crouch method and snapping the camera to the crouch position. If someone could point us in the right direction to setting up a lerp for the crouch function, it would be greatly appreciated.

Hi elpresidenteffg,

Here is the solution that I came up with for smooth crouching. I am using the First Person Template. If you are using the 3rd Person Template it will require some additional steps to add the crouching animations in.

  • Create a variable to hold the CameraLocation and set it in the construction script or OnBeginPlay in the Event Graph.

  • Add an InputAction for crouching (mine is LeftShift).

  • Create a variable for ā€˜IsCrouching?ā€™ This will help if you decide to add in a sprint or something at a later date.

1 Like
  • And here is the setup for the inside of the Timeline.

I hope this help you get on the right track. If you have any questions please feel free to ask.

TJ

Thanks, this is great!

Hello Sir, just wanna mention, when i try to do this, my character becomes a little bit taller, even though iā€™ve tried to tweak the numbers. Iā€™ve done it the exact way as itā€™s shown here without changing the numbers. Any idea what could be wrong?
-EDIT- Just saw that i posted the comment on the wrong picture, iā€™m talking about the whole blueprint script.

Hi Kevin,

Could you post some screenshots of your event graph and timeline?

Hereā€™s the event graph

Hereā€™s the timeline

On your Timline, the starting point is set below zero. Try setting it to 0, 0 and see if that fixes it.

It worked, that was so sloppy of me hehe, iā€™m still new to this though. Thanks

No problem! It happens. Iā€™m glad itā€™s working now. :slight_smile:

Hi, I tried this and it works how I wanted it to, except when I stand up while under something the character just stands and clips through the scenery, getting stuck. Have I missed something, or would this need additional checks to avoid?

Hi spaid,

Iā€™ve been meaning to post back here with an update. Shortly after making this mechanic I had to overcome this same hurdle. Here is the solution that I came up with.

  • Create a Line Trace for Objects that traces above the players head by a set amount. I have it checking on press and release.

  • If it hits an object (mine is set for PhysicsBody & WorldStatic, which is set in the EObject Type variable) it wonā€™t allow the player to stand.

  • I added an optional IsCrouching? variable that is set on Press & Release (this is done for a Sprinting mechanic I plan to have later).

You will notice that if you are crouched and walk out from under something you will not automatically stand up, you will need to tap crouch again to stand. This is because the Line Trace only checks on Press and Release. You could change it to check on a looped Timeline or a TickEvent using Delta Time and change this.

If you have any questions just let me know.

Cheers,

TJ

Thanks TJ, this is working nicely. Though I did need to change it to a sphere trace to stop clipping right at the edge of overhangs.

I was following all your steps and it works perfectly.
Now Iā€™m trying to convert this BP to make it work in multiplayer. The crouching mechanic works fine for the server and client, but as soon as a client goes into crouch, the camera starts stuttering up and down heavily. Iā€™ve already tried several attempts to fix it, but Iā€™m stumped. I would be so glad if anyone could take a look at this.

Here is my (slightly changed) BP:

Did you fixed it?

@TJ Ballard / @spaid
As spaid said, using a line trace can clip you into objects, when you uncrouch right at the edge. I tried his sphere trace and it works better. but Iā€™m still ā€œableā€ to clip into objects at the edge, but only when Iā€™m moving. so when Iā€™m crouching towards an object and uncrouch while still moving I sometimes manage to clip into the object. I think itā€™s because the trace doesnā€™t hit anything when I hit uncrouch but during the crouch transition my location changes, because Iā€™m still moving a few inches. So I kinda understand the problem, but I have no idea how to solve it. Just started with UE last week. Could you guys help me out? Thanks in advance.

@TJ Ballard / @spaid
still havenā€™t figured out a way to get rid of the possibility to clip into objects.
tried to play around with the radius of the trace. but if i set it too high, iā€™m pretty unlikely to clip into the edge of an object above me, but i also canā€™t uncrouch anymore when iā€™m standing right next to a wall without anything above me, which is not wanted.
hereā€™s my setupā€¦

any idea, how to solve this?

hi, I was looking for the smooth crouch implementation and found this helpful. Also I think i fixed the clipping issues and also uncrouhing when under the obstacles. Here are screenshots. Hope this would be helpful. It uses 2 Boolean variables and Custom Events.

Hello! This works really well, except for the fact that the character goes through the ground while I am crouched. I am using the third person template. Any idea on how to fix this ?