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 doublejump = Input.isactionjustpressed("uiaccept") and isfalling
var maxjump = 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
dash()
# 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
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()