#AnimationTreeStateMachine Help

1 messages · Page 1 of 1 (latest)

shrewd elbow
#

Here is what CharacterBody3D code looks like:


var speed
var Running
const WALK_SPEED = 10.0
const SPRINT_SPEED = 20.0
const JUMP_VELOCITY = 7.8
var accelerationx = 5.0
var dead = false

var animation_lock : bool = false

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = 18.8

@onready var camera = $Camera3D
@onready var Hand = $"CanvasLayer/Bare Hands/Hand"
@onready var Reaction = $CanvasLayer/LiveReaction/Reaction
@onready var PlayerTree : AnimationTree = $"CanvasLayer/Bare Hands/AnimationTree"
@onready var StateMachine : PlayerStateMachine = $PlayerStateMachine
    
func _ready():
    Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
    camera.current = true
    PlayerTree.active = true

func _unhandled_input(event: InputEvent) -> void:
    if dead:
        return
    if event is InputEventMouseButton:
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
    elif event.is_action_pressed("ui_cancel"):
        Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
        
    if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
        if event is InputEventMouseMotion:
            rotate_y(-event.relative.x * 0.005)
            camera.rotate_x(-event.relative.y * 0.005)
            camera.rotation.x = clamp(camera.rotation.x, -PI/4, PI/3)
            ```
#
    var input_dir = Input.get_vector("left", "right", "forward", "backward")
    var direction = (camera.transform.basis * transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    if not is_on_floor():
        velocity.y -= gravity * delta

    # Handle Jump.
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = JUMP_VELOCITY
    
    # Handle Sprint.
    if Input.is_action_pressed("sprint") and Input.get_vector("left", "right", "forward", "backward") and StateMachine.check_if_can_run():
        speed = move_toward(speed, SPRINT_SPEED, accelerationx * delta)
    else:
        speed = WALK_SPEED
    
    Running = Input.is_action_pressed("sprint") and Input.get_vector("left", "right", "forward", "backward") and speed == 20

    if is_on_floor():
        if direction:
            velocity.x = direction.x * speed
            velocity.z = direction.z * speed
        else:
            velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
            velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
        
    else:
        velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
        velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
    
    move_and_slide()
    punch()
    update_animation()
        
func update_animation():
    PlayerTree.set("parameters/Running/blend_position", Running)
            
func punch():
    if Input.is_action_pressed("Attack") and StateMachine.check_if_can_punch():
        pass```
#

Here is what my State Machine looks like:


class_name PlayerStateMachine

@export var Player : CharacterBody3D
@export var animation_tree : AnimationTree
@export var CurrentState : State

var states : Array[State]

func _ready():
    for child in get_children():
        if(child is State):
            states.append(child)
            
            #Set the states up with what they need to function
            child.Player = Player
            child.playback = animation_tree["parameters/playback"]
            
        else:
            push_warning("Child " + child.name + " is not a State for PlayerStateMachine")
            
func _physics_process(delta):
    if(CurrentState.next_state != null):
        switch_states(CurrentState.next_state)
        
    CurrentState.state_process(delta)

func check_if_can_run():
    return CurrentState.can_run
    
func check_if_can_punch():
    return CurrentState.can_punch

func switch_states(new_state : State):
    if(CurrentState != null):
        CurrentState.on_exit()
        CurrentState.next_state = null
    
    CurrentState = new_state
    
    CurrentState.on_enter()
    
func _input(event : InputEvent):
    CurrentState.state_input(event)```
#

Here is what my BareHandsState looks like:


class_name Bare_HandsState

@export var punch_state : State 
@export var punch_animation : String = "Punch"

func state_input(event : InputEvent):
    if(event.is_action_pressed("Attack")):
        punch()
        
func punch():
    next_state = punch_state
    playback.travel(punch_animation)```