Godot4 Animation Tree infinite switch back and forth

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tomsliu

When I run the game, the animation does not work properly and the two nodes of the animation tree keep switching back and forth. player code:

extends CharacterBody2D

const SPEED = 300.0

@onready var body = $Body
@onready var animationTree = $AnimationTree

var playback;
var direction:Vector2
var screen:Vector2
var dragging := false
var win_pos:Vector2
var mouse_pos:Vector2
var mouse_in_pos:Vector2
var vel:Vector2

func _ready():
playback = animationTree.get("parameters/playback")
screen = DisplayServer.screen_get_size()

func _input(event):
if event is InputEventMouseButton:
	if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		dragging = true
		mouse_in_pos = get_viewport().get_mouse_position()
	else:
		dragging = false
if event is InputEventMouseMotion and dragging:
	mouse_pos = get_viewport().get_mouse_position()
	get_tree().get_root().position = Vector2(get_tree().get_root().position) + mouse_pos - mouse_in_pos

if event is InputEventKey and event.is_action_pressed("exit"):
	get_tree().quit()
	
func _physics_process(delta):

vel = Vector2.ZERO
direction = Vector2(Input.get_axis("ui_left", "ui_right"), Input.get_axis("ui_up", "ui_down"))
if direction != Vector2.ZERO:
	vel = direction * delta * SPEED
	
win_pos = Vector2(get_tree().root.position) + vel
win_pos.x = clamp(win_pos.x, 0, screen.x)
win_pos.y = clamp(win_pos.y, 0, screen.y)
get_tree().get_root().position = win_pos

pick_new_state()

func pick_new_state():
if dragging:
	playback.travel("Fly")
	print("Fly")
elif (vel != Vector2.ZERO):
	playback.travel("Walk")
	print("Walk")
else:
	playback.travel("Idle")
	print("Idle")

picture link: ThumbSnap - Free Photo & Video Hosting

These animations look like they’re set to auto advance (green arrows). Turn that off by selecting the transition, expand ‘Advance’ in the inspector and change the ‘Mode’ to your desired transition state.

spaceyjase | 2023-05-31 15:07

Thank you so much!!!

tomsliu | 2023-06-01 00:06