I'm learning how to code with Godot, and in the tutorial im following the creator decided to not add a crouching option - which, i decided to do by myself to learn more.
However, for the life of me I can't figure out how to make the character stop moving when they are crouching. I've looked all over the internet and still can't find anything.
Here's my line of code for crouching if it helps

if Input.is_action_pressed("ui_down") and is_on_floor():
	anim.play("Crouch")

Please help!!

EDIT: I finally figured it out! thanks for the help.
Here's how my code looks if anyone is having the same problem! (keep in mind this code also includes the code for the animations :p)

    • Move your if statement higher, to something like line 14.
    • Set a flag if you are crouching, for example is_crouching.
    • If is_crouching is true then do what you need to do while crouching.
    • If it is not true do the other stuff that moves your character around.

    It might be easier to split your code up into separate functions.

    if is_crouching:
        crouch(delta)
    else:
        move_or_jump(delta)

    The way you are doing it now is that you are doing all the jumping and movement first and then check if you are crouching.

Toxe
Like this, i've been playing around with the code a little bit but still can't figure it out

  • Move your if statement higher, to something like line 14.
  • Set a flag if you are crouching, for example is_crouching.
  • If is_crouching is true then do what you need to do while crouching.
  • If it is not true do the other stuff that moves your character around.

It might be easier to split your code up into separate functions.

if is_crouching:
    crouch(delta)
else:
    move_or_jump(delta)

The way you are doing it now is that you are doing all the jumping and movement first and then check if you are crouching.

    reyofnothing unless I've missed some extre feature of godot I think it's just a fancy way of saying "use a boolean"
    var is_crouching = false
    boom, the is_crouching flag now exists

    oh and just in case:
    "if is_crouching:" is just shorthand for "if is_crouching is true:"

      samuraidan
      Thanks! I've been trying this out but it's still not working. When i put the if statement line 14 it still doesnt work (in fact, crouching stops working at all). And i still can't figure out how to implement toxe's suggestion into my code without the code getting damaged ;^^

      okay, meta overview, because I think it might help you:

      you want your movement code to only run when you are not crouching. you have a variable that tracks whether or not you are crouching, and flips between true and not true.

      if you put your movement code underneath an if statement so that it only runs when crouching is false (you are not crouching), what will happen?

        samuraidan
        I did that and it works now! Thank you so much!!!
        I also moved move_and_slide() so it's connected to the if statement, otherwise if i started crouching while moving it'd slide.
        Again, thanks a lot for the help! Got to learn something new 💥