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

I've coded a dash for my 2d platformer, and as far as I can see, the code is right. The dash is kind of working too, but it doesn't work if I've pressed any other key. So, if I press any other key, it doesn't work, which is not desirable.

I'd be most grateful if any of you guys would be so kind to help me out. I want to know what the problem is with the dash. Thanks in advance!!

The code is as follows:

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = 500

var isjumpcancelled = Input.isactionjustreleased("uiaccept") and velocity.y < 0.0
var isfalling = velocity.y > 0.0
var double
jump = Input.isactionjustpressed("uiaccept") and isfalling
var max
jump = 2

var canDash = true
var dashing = false
var dashDirection = Vector2.ZERO

func dash():
if isonfloor():
canDash = true

if Input.is_action_just_pressed("Dash") and canDash:
    velocity = dashDirection.normalized() * 3000
    canDash = false
    dashing = true
    await get_tree().create_timer(1).timeout
    dashing = false

func physicsprocess(delta):
# Add the gravity.
if not isonfloor():
velocity.y += gravity * delta

if is_on_floor():
    max_jump = 0

if Input.is_action_pressed("Right"):
    dashDirection = Vector2(1,0)
if Input.is_action_pressed("Left"):
    dashDirection = Vector2(-1,0)

# Handle Jump.
if Input.is_action_just_pressed("Jump") and is_on_floor():
    velocity.y = JUMP_VELOCITY
    max_jump += 1

const double_jump_velocity = -600

if not is_on_floor() and max_jump < 2:
    if Input.is_action_just_pressed("Jump"):
        velocity.y += double_jump_velocity
        max_jump += 1

if is_jump_cancelled:
    velocity.y = 0.0


# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("Left", "Right")
if direction:
    velocity.x = direction * SPEED
    velocity.x = move_toward(velocity.x, 0, SPEED)

Godot version Godot 4
in Engine by (12 points)

Please log in or register to answer this question.

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.