+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

## 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.

http://imgur.com/a/JiMKa

by (810 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_JUST_PRESSED  = 1
var INPUT_PRESSED       = 2
var INPUT_JUST_RELEASED = 3

_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 (118 points)