OccasionalCrust

  • 21 May
  • Joined 18 May
  • 0 best answers
  • HerbalHerb Thank you for your answer. I managed to fix it on my own after some time, I noticed the logic issue eventually lol.

    What is a state machine? I am unfamiliar

    • Toxe replied to this.
    • Hello,

      I am trying to make a 2D platformer Character Controller, and there's two movement mechanics I have had a lot of trouble getting to work in unison: that being, variable jump height and double jumping. What I mean by variable jump height is that your jump height depends on how long you hold down the jump button. I guess my question is, why shouldn't this work?

      var can_jump: bool = false
      var can_double_jump: bool = false
      var jump_timer: float = 0
      var jump_force: float = 200

      func jump(force):
      > velocity.y = -force

      func _physics_process(delta: float) -> void:
      > if is_on_floor():
      >> can_jump = true
      >> can_double_jump = true
      >
      > if Input.is_action_just_pressed("jump"):
      >> if can_jump && can_double_jump:
      >>> can_jump = false
      >> if can_jump == false && can_double_jump:
      >>> can_double_jump = false
      >
      > if Input.is_action_pressed("jump") && jump_timer < 0.25 && (can_jump or can_double_jump):
      >> jump(jump_force)
      >> jump_timer += delta
      >
      >if Input.is_action_just_released("jump"):
      >> jump_timer = 0

      Thank you for your help, hopefully this code is readable. I am just getting into game dev and I'm excited to hear feedback!

      • Applying jump force at the start of every jump and cutting velocity on jump release is the most common way to implement it. You're implementation doesn't work because you set your checks to false, then check again to apply jump velocity. Logic pipeline should be

        1. Listen for jump input from floor
        2. Apply jump velocity and set can_jump to false and start timer
        3. on timer timeout or timer > max_length check jump input again. --> still pressed do nothing. --> not pressed cut vertical velocity

        Double jump doesn't interfere with this functionality as long as you reset the timer on action pressed + not on floor + can_double_jump.

        A state machine would make this even easier because even with this small example you're already drowning in boolean checks and conditionals that would much rather live in state transitions.