- Here all the player script:
extends CharacterBody3D
@export var speed = 4.0
@export var jump_impulse = 18.0
@export var acceleration = 2
@export var fall_acceleration = 55.0
@export var state_machine : LimboHSM
# All states.
@onready var idle_state = $LimboHSM/Idle
@onready var walk_state = $LimboHSM/Walk
@onready var run_state = $LimboHSM/Run
@onready var jump_state = $LimboHSM/Jump
@onready var fall_state = $LimboHSM/Fall
@onready var roll_state = $LimboHSM/Roll
@onready var kick_state = $LimboHSM/Kick
@onready var block_state = $LimboHSM/Block
@onready var attack1_state = $LimboHSM/Attack1
@onready var attack2_state = $LimboHSM/Attack2
@onready var attack3_state = $LimboHSM/Attack3
# State Machine.
func _ready():
_initialize_state_machine()
# Input for idle_state.
var movement_input : Vector2 = Vector2.ZERO
func _initialize_state_machine():
# Setup.
state_machine.initial_state = idle_state
state_machine.initialize(self)
state_machine.set_active(true)
# Transitions.
state_machine.add_transition(state_machine.ANYSTATE, idle_state, "to_idle")
state_machine.add_transition(state_machine.ANYSTATE, walk_state, "to_walk")
state_machine.add_transition(state_machine.ANYSTATE, jump_state, "to_jump")
state_machine.add_transition(state_machine.ANYSTATE, fall_state, "to_fall")
state_machine.add_transition(walk_state, run_state, "to_run")
state_machine.add_transition(idle_state, block_state, "to_block")
state_machine.add_transition(walk_state, block_state, "to_block")
state_machine.add_transition(run_state, block_state, "to_block")
state_machine.add_transition(idle_state, attack1_state, "to_attack1")
state_machine.add_transition(walk_state, attack1_state, "to_attack1")
state_machine.add_transition(block_state, attack1_state, "to_attack1")
state_machine.add_transition(attack1_state, attack2_state, "to_attack2")
state_machine.add_transition(attack2_state, attack3_state, "to_attack3")
state_machine.add_transition(run_state, attack3_state, "to_attack3")
# End of animations.
func _on_animation_player_animation_finished(anim_name: StringName) -> void:
if anim_name == "Attack1":
state_machine.dispatch("to_idle")
elif anim_name == "Attack2":
state_machine.dispatch("to_idle")
elif anim_name == "Attack3":
state_machine.dispatch("to_idle")
# Physic of the world.
func _physics_process(delta: float) -> void:
var direction : Vector2 = Input.get_vector("left", "right", "down", "up")
var direction3 := Vector3(direction.x, 0, -direction.y)
# Input State Machine.
movement_input = direction
# Move and rotate the character in direction.
if direction != Vector2.ZERO:
if Input.is_action_pressed("speed"):
rotation.y = lerp_angle(rotation.y, direction.angle(), 0.3)
elif Input.is_action_pressed("block"):
rotation.y = lerp_angle(rotation.y, direction.angle(), 0.1)
else:
rotation.y = lerp_angle(rotation.y, direction.angle(), 0.2)
# Ground velocity.
if Input.is_action_pressed("speed"):
velocity.x = direction3.x * speed * acceleration
velocity.z = direction3.z * speed * acceleration
else:
velocity.x = direction3.x * speed
velocity.z = direction3.z * speed
if is_on_floor() and Input.is_action_pressed("block"):
velocity.x = 0
velocity.z = 0
# Jumping.
if is_on_floor() and Input.is_action_just_pressed("jump"):
velocity.y = jump_impulse
# Air velocity.
if not is_on_floor():
velocity.y = velocity.y - (fall_acceleration * delta)
move_and_slide()
# Reload scene.
if position.y < -30:
get_tree().reload_current_scene()
- Here the idle state script:
extends LimboState
@export var animation_player : AnimationPlayer
@export var animation : StringName
# Called when the node enters the scene tree for the first time.
func _enter() -> void:
animation_player.play(animation)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
func _update(delta: float) -> void:
agent._physics_process(delta)
if agent.movement_input != Vector2.ZERO:
get_root().dispatch("to_walk")
if Input.is_action_just_pressed("jump"):
get_root().dispatch("to_jump")
if agent.velocity.y < 0:
get_root().dispatch("to_fall")
if Input.is_action_pressed("block"):
get_root().dispatch("to_block")
if Input.is_action_just_pressed("attack"):
get_root().dispatch("to_attack1")
extends LimboState
@export var animation_player : AnimationPlayer
@export var animation : StringName
# Called when the node enters the scene tree for the first time.
func _enter() -> void:
animation_player.play(animation)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
func _update(_delta: float) -> void:
pass
But I do not know if that can help…
I precise that I am a new comer in the coding world so lot of things are still confusing! For implement this state machine with this addon I followed the Chap.C Creates video: