Toggle "C" key to make player crouch

I’m making a platformer game and was curious if there is a way to make it where the “c” key toggles the player to crouch.

If you have a boolean variable is_crouching, then you can create an event with the conditions:

C key is pressed” and
Trigger once while true”,

and in the actions

Toggle boolean value is_crouching

.

is_crouching will then flip from true to false and back every time the C key is pressed.

1 Like

I tried that, idk what I’m doing wrong, but it just goes to crouch then instantly back to the idle animation.

This is the code I am currently using.

Yeah, I can see one potential logic issue that could cause it.

From your screen shot, the second event sets STANDING_SHOOT_ANIM to 0 if left mouse button is not held down. The next event will then set the animation to RUN.

Then, in the first events of the movement animation event group, since the animation is RUN, the 3 conditions are met, and since the soldier probably isn’t moving, the animation is set to iDLE.

i guess if you releas C key player shouldnt crouch…
try without trigger once

That won’t work because then the boolean variable CROUCHED will get changed every single frame (or about 60 times a second).

have you tried without sub events?.. i mean remove them from the toggle boolean condition, since it looks like if C is not pressed theyre not gonna happen

The only reason you’d do that is if CROUCHED could be updated in another event. But since its not, the sub events are fine. They only need to get checked when the value is toggled.

I’m not completely sure,…what i read is that if C is not pressed the sub events not gonna happen, it looks like a contraddiction, maybe i’m gonna test it later…also it looks like there is no return condition to false for the boolean var…at least from the screenshots…i don’t see if c is not pressed reset the boolean to false,
anyway imho you shouldn’t use a boolen to check a keypress…keypress is enough.

Because my understanding is that’s not what OP wanted to achieve. The request was to toggle, not make, the player crouch. The start of this thread was:

That is a different matter.
If he’s using a platform behavoiur he should first uncheck the default key option form the behaviour before use different keys. But imo there is stil something wrong in the way he put the conditions

I have had this problem before and I just added a wait 0.2 secends and it work but that will add a delay to the crouching so I Dont know if that will effect your game at all

while it should works sometimes, i suggest you all to never use timers to check inputs press.
They’re gonna cause you a lot of problems later.
There are a lot of other methods to achieve that. In this particular situation that is not needed since the OP want to just crouch when C button is pressed.

So first he should disable standard keys from platform behaviour, (if he’s using it) second i suggest to remove the boolean check since i’ve never seen a platformer game where the player crouch and stay crouched after crouch button released, but even so he just need to fix his conditions cos are bad written

Here’s my point:
If you have an animation that make the player crouch, try this:
condition
look for keyboard in other conditions, then look for when key pressed and then, press c and select only c.

ACTION
Check for the player’s animation by name then use the crouch animation.

If you want the player to be idle after crouching, you can use the same condition, then invert it, and for action use the animation for idle.

I hope I was able to help.