My character cant dash if he is currently moving

Godot Version

godot 4.3

Question

if my character is moving and i click dash it wont dash for some reason can someone please help with this?
the code:

extends CharacterBody2D

var isLeft = false
var Dashing = false
const SPEED = 600.0
const JUMP_VELOCITY = -700.0
const SprintMultiplier = 2
var doubleJump = true
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * 2 * delta
if is_on_floor():
doubleJump = true

# Handle jump.
if Input.is_action_just_pressed("jump"):
	if is_on_floor():
		velocity.y = JUMP_VELOCITY
	if not is_on_floor() and doubleJump:
		velocity.y = JUMP_VELOCITY
		doubleJump = false

# 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")
var sprint = Input.is_action_pressed("sprint")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)


if direction and sprint:
	velocity.x = direction * SPEED * SprintMultiplier
move_and_slide()

if velocity.x > 0:
	isLeft = false
elif velocity.x < 0:
	isLeft = true

if Input.is_action_just_pressed("Dash") and not Dashing:
	Dash()

func Dash():
Dashing = true
var DashDirection = 1
if isLeft == true:
DashDirection = -1
else:
DashDirection = 1
velocity.x = DashDirection * SPEED * 10
Dashing = false

There’s no check that the player is dashing before applying the normal movement speed.

to each of the statements i just added to the if statements to start movement “and not Dashing” which states that the player is dashing if true it still doesnt work

What is your code now?

extends CharacterBody2D

var isLeft = false
var Dashing = false
const SPEED = 600.0
const JUMP_VELOCITY = -700.0
const SprintMultiplier = 2
var doubleJump = true
func _physics_process(delta):

Add the gravity.

if not is_on_floor():
velocity += get_gravity() * 2 * delta
if is_on_floor():
doubleJump = true

Handle jump.

if Input.is_action_just_pressed(“jump”):
if is_on_floor():
velocity.y = JUMP_VELOCITY
if not is_on_floor() and doubleJump:
velocity.y = JUMP_VELOCITY
doubleJump = false

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”)
var sprint = Input.is_action_pressed(“sprint”)
if direction and not Dashing:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

if direction and sprint and not Dashing:
velocity.x = direction * SPEED * SprintMultiplier
move_and_slide()

if velocity.x > 0:
isLeft = false
elif velocity.x < 0:
isLeft = true

if Input.is_action_just_pressed(“Dash”) and not Dashing:
Dash()
func Dash():
Dashing = true
var DashDirection = 1
if isLeft == true:
DashDirection = -1
else:
DashDirection = 1
velocity.x = DashDirection * SPEED * 10
Dashing = false

Make sure to paste your script with proper formatting, between three ticks ```

1 Like

you need to add the dash check to the else statements as well

Did that still doesnt work

extends CharacterBody2D

var isLeft = false
var Dashing = false
const SPEED = 600.0
const JUMP_VELOCITY = -700.0
const SprintMultiplier = 2
var doubleJump = true

func _ready():
ExtraVariables.Player = self

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * 2 * delta
if is_on_floor():
doubleJump = true

# Handle jump.
if Input.is_action_just_pressed("jump"):
	if is_on_floor():
		velocity.y = JUMP_VELOCITY
	if not is_on_floor() and doubleJump:
		velocity.y = JUMP_VELOCITY
		doubleJump = false

# 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")
var sprint = Input.is_action_pressed("sprint")
if direction and not Dashing:
	velocity.x = direction * SPEED
else:
	if not Dashing:
		velocity.x = move_toward(velocity.x, 0, SPEED)


if direction and sprint and not Dashing:
	velocity.x = direction * SPEED * SprintMultiplier
move_and_slide()

if velocity.x > 0:
	isLeft = false
elif velocity.x < 0:
	isLeft = true

if Input.is_action_just_pressed("Dash") and not Dashing:
	Dash()

func Dash():
Dashing = true
var DashDirection = 1
if isLeft == true:
DashDirection = -1
else:
DashDirection = 1
velocity.x = DashDirection * SPEED * 10
Dashing = false