Godot 4.4
Could any help me make state machine? im still pretty new bad in coding ![]()
extends CharacterBody2D
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
# list of Current States
enum States {IDLE, WALKING, RUNNING, FLYING, FLYFORWARD, INTERACTING, TALKING}
# This variable keeps track of the character's current state.
var state: States = States.IDLE
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var flyf= false
var flip_h = false
func _ready():
$AnimatedSprite2D.play("Idle")
func _physics_process(delta: float) -> void:
# Add the gravity. and animtion for flying
if not is_on_floor():
velocity += get_gravity() * delta
#$AnimatedSprite2D.play("Flying")
# For player to Interact with other Objects
if Input.is_action_just_pressed("Interact"):
state == States.INTERACTING
interacting = true
$AnimatedSprite2D.play("Interact")
for area in $InteractZone.get_overlapping_areas():
if area.has_method("on_interact"):
area.on_interact(self)
else: state == States.IDLE
# Handle Flying.
if Input.is_action_just_pressed("Fly"):
state == States.FLYING
velocity.y = JUMP_VELOCITY
animated_sprite.play("Fly")
var direction = Input.get_axis("Walk Left", "Walk Right")
# Trying to make if in FLying "not on floor" and press Left or RIght Animation play flying forward and character move
elif, not is_on_floor() Input.is_action_pressed("Walk Left") or Input.is_action_pressed("Walk Right"): # not working Error, EXPECTED conditional expression after Elif
state == states.FLYFORWARD:
velocity.x = direction * SPEED
elif, is_on_floor() and Input.get_axis("Walk Left", "Walk Right"): # not working same problem
state == States.WALKING
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
if state == States.WALKING:
var direction = Input.get_axis("Walk Left", "Walk Right")
# for animation and coillsion fliping
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if Input.is_action_pressed("Walk Left"):
$AnimatedSprite2D.flip_h = true
$CollisionPolygon2D.scale.x = -1
%CollisionShape2D.position.x = -261.0
if Input.is_action_pressed("Walk Right"):
$AnimatedSprite2D.flip_h = false
$CollisionPolygon2D.scale.x = 1
%CollisionShape2D.position.x = -41.0
move_and_slide()