This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes
extends KinematicBody2D

const DASH_SPEED = 2000
const MAX_SPEED = 128
const ACCEL = 512
const FRICTION = 0.30
const GRAVITY = 600
const JUMP_FORCE = 200
const AIR_RESISTANCE = 0.02

var vel = Vector2.ZERO

onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer

func run(directionstrength, delta):
    animationPlayer.play("Run")
    vel.x += directionstrength * ACCEL * delta
    vel.x = clamp(vel.x, -MAX_SPEED, MAX_SPEED)
    sprite.flip_h = directionstrength < 0 

func jump():
    vel.y = -JUMP_FORCE

func applyfriction():
    vel.x = lerp(vel.x, 0 , FRICTION) 

func jumpRelease():
    vel.y = -JUMP_FORCE/2

func dash(directionstrength):
    if directionstrength > 0:
        vel.x += DASH_SPEED
    elif directionstrength < 0:
        vel.x += -DASH_SPEED
    else:
        vel.x = 0

func _physics_process(delta):

    vel.y += GRAVITY * delta
    var x_direction = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")

    if x_direction != 0:
        run(x_direction, delta)
    else:
        animationPlayer.play("Idle")

    if is_on_floor(): 
        if x_direction == 0:
            applyfriction()

        if Input.is_action_just_pressed("jump"):
            jump()

    else:
        animationPlayer.play("Jump")
        if Input.is_action_just_released("jump") and vel.y < -JUMP_FORCE/2:
            jumpRelease()

        if x_direction == 0:
            vel.x = lerp(vel.x, 0 , AIR_RESISTANCE) 

        if Input.is_action_just_pressed("dash"):
            dash(x_direction)

    vel = move_and_slide(vel, Vector2.UP)

How do I make it so that my dash only works one time and resets when you jump again. I cant wrap my head around it. Thanks for the help and sorry for the bad code.

in Engine by (12 points)

2 Answers

0 votes

Have a variable, like can_dash and set it to false once the player uses the one air-dash. Only let the player dash if its value is true. Once the player jumps again, set its value to true so the player can dash again.

by (8,580 points)
0 votes

Better to use a Finite State Machine for this purpose.

by (895 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 Frequently asked questions and 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 [email protected] with your username.