@export var animation_tree : AnimationTree
var input : Vector3
var playback : AnimationNodeStateMachinePlayback
func _ready():
playback = animation_tree["parameters/playback"]
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _physics_process(delta):
# Add the gravity.
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("Left", "Right", "Back", "Front")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if 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)
$AnimationTree.set("parameters/conditions/idle", input_dir == Vector3.ZERO && is_on_floor())
$AnimationTree.set("parameters/conditions/walk", input_dir != Vector3.ZERO && is_on_floor())
move_and_slide()
select_animation()
update_animation_parameters()
func select_animation():
if velocity == Vector3.ZERO:
playback.travel("Idle")
else:
playback.travel("Walk")
func update_animation_parameters():
if input == Vector3.ZERO:
return
animation_tree["parameters/Idle/blend_position"] = input
animation_tree["parameters/Walk/blend_position"] = input