+1 vote

Hi all,

Chasing some logic ideas here. I'm pretty new at this.

I have a character running, jumping, etc. I want to implement a "dash" function to the character.

Pressing R makes character run, tapping and holding R again makes character dash.

I have a state setup happening where I have all my code in the R key as such:

    if btn_right.check() == 2:  #2 = pressed in the input_states.gd script
    move(player_speed, acceleration, delta)
    ORIENTATION_NEXT = "right"
    anim = "run"
    anim_speed = 2
    anim_blend = 0.3

How would I go about putting a check in there for dashing or not?
I've considered these variables:

#dash variables
var dash = false  #are they dashing?
var dash_time = 4  #how long they can dash for
var dash_speed = 2 #multiplier - how much faster than the normal run is the dash

I'm just at a loss of how to say and where to put something like:
- check if R is pressed a second time
- if so, then dash like a madpixelsprite
- if key released, set dash to FALSE

I know I've almost answered it, I just can't get it right unfortunately. Any help would be manna from heaven. :-)

in Engine by (823 points)

2 Answers

0 votes

For future reference, this is where I'm heading.

I am thinking that when the timer reaches 0 it will reset the dash_count to 0.


by (823 points)

You're heading in the right direction. Added my solution below

0 votes

You're heading in the right direction. The following should help assuming you're using inputstates.gd from ndee85 since you had btnright.check(). The code has not been tested but I used a similar piece on a Unity project. The logic is the same so it should work.

var dashing          = false   # whether player is dash or not
var max_dash_delay   = 0.5     # time between keypress (.5 secs)
var max_dash_time    = 2.0     # how long player can run for (2 secs)
var dash_timer       = 0       # timer to reset dash state
var dash_press_count = 0       # keep track of double tap count
var move_speed       = 5       # the normal move speed
var dash_speed       = 10      # the dash speed
var speed            = 0       # used to store whichever speed the player is moving at
var velocity                   # the velocity which governs the player's movement

# using ndee85 state values [ 0 - released, 1 - just pressed, 2 - pressed, 3 - just released ] below for readability
var INPUT_RELEASED      = 0
var INPUT_PRESSED       = 2

_ready() :
    speed = move_speed    # default to the move speed
    velocity = Vector2()  # default velocity

_process(delta) :

    # only attempt to dash if the player's not dashing
    if (dash_action.check() == INPUT_JUST_PRESSED and not dashing) :
        dash_press_count += 1

    # start the dash timer when player just presses the dash action
    if (dash_press_count > 0 and not dashing) :
        dash_timer += delta

        # the player took too long to perform the second tap so reset values
        if (dash_timer > max_dash_delay) :
            dash_timer = 0
            dash_press_count = 0

    # the player managed to dash successfully
    if (dash_press_count >= 2 and not dashing) :
        dashing = true
        dash_timer = 0
        dash_press_count = 0

        # player now uses the dash speed until timer elapses
        speed = dash_speed

    # the player will continue to dash until the timer elapses
    if (dashing) :
        dash_timer += delta
        if (dash_timer >= max_dash_time) :
            dashing = false
            dash_timer = 0

            # timer elapsed, reset to the normal speed
            speed = move_speed

    # move using whichever speed is chosen
    velocity += Vector2( speed * delta, velocity.y )

    # assuming you're using kinematicbody2d otherwise use set_pos(velocity)
    move (velocity) 
by (122 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.