I just started using godot a couple weeks ago and have been making movement code using youtube videos and have made some basic movement code I'm satisfied with for the time being. I was watching a video on making state machines (this one) [ as from what I've seen they seem to be necessary when it comes to making things like attacks and wall jumps. And I can't understand how to transfer my movement code and animations into the state machine.
If anyone has any tips on how to make the state machine function that would be very appreciated.
-Movement code
extends CharacterBody2D
@export var speed : float = 100.0
@export var jump_velocity : float = -250.0
@export var acceleration : int = 50
@onready var movement_sprite : AnimatedSprite2D = $Idle
@onready var Heavy_Punch_sprite : AnimatedSprite2D = $HeavyPunch
@export var jump_buffer_time: int = 15
@export var coyote_time: int = 15
var jump_buffer_counter : int = 0
var coyote_counter: int = 0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation_locked : bool = false
var direction : Vector2 = Vector2.ZERO
func _physics_process(delta):
#Buffer for walking off ledges
if is_on_floor():
coyote_counter = coyote_time
# Add the gravity.
if not is_on_floor():
if coyote_counter > 0:
coyote_counter -= 1
velocity.y += gravity * delta
if velocity.y > 2000:
velocity.y = 2000
# Handle Jump.
if Input.is_action_just_pressed("Up"):
jump_buffer_counter = jump_buffer_time
if jump_buffer_counter >0:
jump_buffer_counter -= 1
if jump_buffer_counter > 0 and coyote_counter > 0:
velocity.y = jump_velocity
jump_buffer_counter = 0
coyote_counter = 0
if Input.is_action_just_released("Up"):
if velocity.y < 0:
velocity.y += gravity * 0.1
if Input.is_action_pressed("Down") and not is_on_floor():
if velocity.y > 0 and not is_on_floor():
velocity.y += gravity * 0.01
direction = Input.get_vector("Left", "Right", "Up", "Down")
if direction:
if direction.x > 0:
direction.x = 1
if direction.x < 0:
direction.x = -1
velocity.x = direction.x * speed * acceleration
else:
velocity.x = move_toward(velocity.x,0,5)
velocity.x = clamp(velocity.x, -speed, speed)
move_and_slide()
update_animation()
func update_animation():
if not animation_locked:
if direction.x != 0:
movement_sprite.play("Run")
else: movement_sprite.play("Idle")
if animation_locked or not animation_locked:
Heavy_Punch_sprite.hide()
if Input.is_action_pressed("Punch"):
Heavy_Punch_sprite.show()
Heavy_Punch_sprite.play("HeavyPunch")
movement_sprite.hide()
if Heavy_Punch_sprite.animation_finished:
Heavy_Punch_sprite.hide()
movement_sprite.show()
movement_sprite.play("Idle")
if direction.x != 0:
movement_sprite.play("Run")
else: movement_sprite.play("Idle")
-CharacterBody2D Code
class_name GreaterBushy
extends CharacterBody2D
@export var speed : float = 100.0
@export var jump_velocity : float = -250.0
@export var acceleration : int = 50
@onready var movement_sprite : AnimatedSprite2D = $Idle
@onready var Heavy_Punch_sprite : AnimatedSprite2D = $HeavyPunch
#Buffers for jumps after landing and walking off ledges
@export var jump_buffer_time: int = 15
@export var coyote_time: int = 15
var jump_buffer_counter : int = 0
var coyote_counter: int = 0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation_locked : bool = false
var direction : Vector2 = Vector2.ZERO
-State Machine
class_name StateMachine
extends Node
@export var state : State
func _ready():
change_state(state)
func change_state(new_state: State):
if state is State:
state._exit_state()
new_state._enter_state()
state = new_state
-Idle State
class_name IdleState
extends State
signal NotMoving
@export var actor: GreaterBushy
@export var animator:AnimatedSprite2D
@export var speed : float = 100.0
@export var jump_velocity : float = -250.0
@export var acceleration : int = 50
@onready var movement_sprite : AnimatedSprite2D = $Idle
#Buffers for jumps after landing and walking off ledges
@export var jump_buffer_time: int = 15
@export var coyote_time: int = 15
var jump_buffer_counter : int = 0
var coyote_counter: int = 0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation_locked : bool = false
var direction : Vector2 = Vector2.ZERO
func _ready():
set_physics_process(false)
func _enter_state() -> void:
set_physics_process(true)
movement_sprite.play("Idle")
func _exit_state() -> void:
set_physics_process(false)
func _physics_process(delta):
movement_sprite.play("Idle")