Godot Version
extends CharacterBody3D
class_name Player
@onready var animation_tree: AnimationTree = $AnimationTree
@onready var animated_sprite_3d: AnimatedSprite3D = $AnimatedSprite3D
var last_direction = Vector3.BACK
var state_machine : AnimationNodeStateMachinePlayback
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _ready() → void:
state_machine = $AnimationTree.get(“parameters/AnimationNodeStateMachine/playback”) as AnimationNodeStateMachinePlayback
func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
last_direction = direction
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
if velocity.length_squared() > 0 and is_on_floor():
$AnimationTree.set("parameters/AnimationNodeStateMachine/conditions/walking", true)
$AnimationTree.set("parameters/AnimationNodeStateMachine/conditions/idle", false)
else:
$AnimationTree.set("parameters/AnimationNodeStateMachine/conditions/walking", false)
$AnimationTree.set("parameters/AnimationNodeStateMachine/conditions/idle", true)
move_and_slide()
Question
I cannot get this animation tree to work, i genuinely have no idea what to do anymore. I’m trying to make a 2.5D game where the character is 2D but the world is 3D. I imported all my animations into the AnimationPlayer mapped them to a BlendSpace 2D inside a StateMachine but for my state machine code never works. Ive watched countless tutorials but nothing seems to work. I think it might be because of my movement code, but im not sure. I’m very, VERY new to coding but Ive spent like 25 hours on this issue alone. I genuinely need help lmao, figured Id post something and see.