Dashing with a finite state machine

godot 4

class_name Dashstate
extends Player_State
#-------------------------------------------
@export var fall_state : Player_State
@export var ground_state : Player_State
@export var run_animation_node : String = "Run"
@export var fall_animation_node : String = "Fall"
#-------------------------------------------
@onready var timer : Timer = $Timer
#-------------------------------------------
const dash_speed = 1
#-------------------------------------------
var dashing : bool = false
#-------------------------------------------

func state_process(delta):
	timer.start()
	dashing = true
	#var direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down")
	#character.velocity.x = direction.x * dash_speed
	if (timer.is_stopped() == true and character.is_on_floor()):
		dashing = false
		playback.travel(run_animation_node)
		next_state = ground_state
	elif(timer.is_stopped() == true and character.is_on_floor() == false):
		dashing = false
		playback.travel(fall_animation_node)
		next_state = fall_state
#-------------------------------------------



Timer is set at 0,2 seconds and oneshot == true

Is this even possible? When I try to make a dash it seems to be overwritten by the player movement logic. Is there any way to fix this?

any help is greatly appreciated, thank you in advance

I assume you call state_process every frame? If so, timer.start() will reset the timer each frame to 0.2 seconds. The if conditions will never hold true.

I figured it out! Thank you so much man I don’t think i would have figured it out with out this

Solution:

class_name Dashstate
extends Player_State
#-------------------------------------------
@export var fall_state : Player_State
@export var ground_state : Player_State
@export var dash_animation : String = "Dash"
@export var run_animation_node : String = "Run"
@export var fall_animation_node : String = "Fall"
#-------------------------------------------
@onready var timer : Timer = $Timer
#-------------------------------------------
const dash_speed = 500
#-------------------------------------------
var dashing : bool = false
#-------------------------------------------

func on_enter():
	timer.start()
	
	
func state_process(delta):
	var direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down")
	character.SPEED = dash_speed 
	print(character.SPEED)


func _on_timer_timeout():
	if (character.is_on_floor()):
		dashing = false
		playback.travel(run_animation_node)
		next_state = ground_state
	elif(character.is_on_floor() == false):
		dashing = false
		playback.travel(fall_animation_node)
		next_state = fall_state


1 Like