I am very new to godot and gdscript and Ive hit a roadblock relating to my first state machine on my first game. Im trying to make a basic movement system made up of 4 movements: left boost, right boost(these would automatically move the player a certain distance while allowing for vertical movement and would not be a teleporter), dodge(temporarily disables collision while still allowing for movement), and normal movement(up, down, left, and right). Im trying to make my game similar to the flight sections of cuphead so theres no gravity.
My issue is that the animations do not respond to key inputs as their supposed to and only the normal movement seems to be working. This probably has a very simple solution but I cant figure it out. I dont have any programming experience outside of high school java courses.
extends CharacterBody2D
enum States {DODGE, LEFT_BOOST, RIGHT_BOOST, MOVE}
var speed = 200
@onready var timer: Timer = $Timer
@onready var collision: CollisionShape2D = $head_target
@onready var animation: AnimatedSprite2D = $animations
var state: States = States.MOVE
func set_state_animation(new_state: States) -> void:
state = new_state
match state:
States.DODGE:
animation.play("dodge")
States.LEFT_BOOST:
animation.play("left boost")
States.RIGHT_BOOST:
animation.play("right boost")
States.MOVE:
animation.play("idle")
func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("dodge"):
state == States.DODGE
elif Input.is_action_just_pressed("left boost"):
state == States.LEFT_BOOST
elif Input.is_action_just_pressed("right boost"):
state == States.RIGHT_BOOST
elif Input.is_action_just_pressed("right") or ("left") or ("up") or ("down"):
state = States.MOVE
func _physics_process(delta: float) -> void:
if state == States.DODGE:
timer.start()
collision.disabled = true
if timer.time_left <= 0:
state == States.MOVE
collision.disabled = false
elif state == States.LEFT_BOOST:
velocity.x = 0
position.x -= delta * speed * 2
velocity.y = speed
move_and_slide()
if position.x <= -2 * speed:
state == States.MOVE
elif state == States.RIGHT_BOOST:
velocity.x = 0
position.x += delta * speed * 2
velocity.y = speed
move_and_slide()
if position.x >= 2 * speed:
state == States.MOVE
elif state == States.MOVE:
var X_axis := Input.get_axis("left", "right")
if X_axis:
velocity.x = X_axis * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
move_and_slide()
var Y_axis := Input.get_axis("up", "down")
if Y_axis:
velocity.y = Y_axis * speed
else:
velocity.y = move_toward(velocity.y, 0, speed)
move_and_slide()