Hi everyone,
Below is my movement and dash code for a 2D game I'm working on. Right now, the player can only dash when the player is moving, which is what I want. However, when the player dashes, they can still move freely while in the dash motion (for example, if I'm moving right when I push dash, while dashing, the player can also move up). I'm wanting to make it so that when they dash, they can only dash in the direction of the last input when the dash button was pushed (for example, if I'm moving up/right, push dash, then my player is only moving up/right) - kind of like in Celeste
extends KinematicBody2D
# Movement properties
var move_right_action := "move_right"
var move_left_action := "move_left"
var move_down_action := "move_down"
var move_up_action := "move_up"
var can_move := true
export var normal_speed := 300.0
var speed := normal_speed
var move_input := Vector2.ZERO
var velocity := Vector2.ZERO
var last_move_input := Vector2.ZERO
# Dash properties
export var dash_speed := 500.0
export var dash_duration := 2
var can_dash := true
func _physics_process(_delta: float) -> void:
move()
dash()
func move() -> void:
if can_move:
move_input.x = Input.get_action_strength(move_right_action) - Input.get_action_strength(move_left_action)
move_input.y = Input.get_action_strength(move_down_action) - Input.get_action_strength(move_up_action)
if move_input.length() > 1.0:
move_input = move_input.normalized()
velocity = move_input * speed
last_move_input = move_input
move_and_slide(velocity)
func dash():
if Input.is_action_just_pressed("dash") && can_dash && !is_dashing():
if last_move_input != Vector2.ZERO:
start_dash(dash_duration, last_move_input)
func start_dash(dash_duration, direction):
$DurationTimer.wait_time = dash_duration
$DurationTimer.start()
speed = dash_speed
velocity = direction * speed
move_and_slide(velocity)
func is_dashing():
return !$DurationTimer.is_stopped()
func _on_DurationTimer_timeout() -> void:
# After timeout, call for cant_dash
cant_dash()
func cant_dash():
# After dash, player can't dash or move again
can_dash = false
can_move = false
Any help here would be appreciate, as I can seem to figure it out.