You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDScript调用update_animation_parameters函数出现解析错误求助

修复GDScript解析错误及全方向动画问题

错误原因

你遇到的Parse Error: Unexpected token: Identifier:update_animation_parameters,核心问题是调用update_animation_parameters时传入了未定义的变量input_direction,GDScript无法识别这个标识符,从而触发解析错误。

修复步骤

  1. 替换未定义变量:将update_animation_parameters(input_direction)改为update_animation_parameters(velocity)——velocity正是你根据输入生成的方向向量,完全符合动画参数更新的需求。
  2. 逻辑合理性确认:当前代码中调用动画更新函数的时机已经正确(输入处理完成后),无需额外调整位置。

修复后的完整代码

extends KinematicBody2D
export (int) var speed = 150
export var starting_direction : Vector2 = Vector2(0, 1)

var velocity = Vector2()

onready var animation_tree = $AnimationTree

func _ready():
    animation_tree.set("parameters/Idle/blend_position", starting_direction)

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    
    # 传入正确的方向向量velocity,替代未定义的input_direction
    update_animation_parameters(velocity)
    
    velocity = velocity.normalized() * speed

func _physics_process(_delta):
    get_input()
    velocity = move_and_slide(velocity)
    
func update_animation_parameters(move_input : Vector2):
    if move_input != Vector2.ZERO:
        animation_tree.set("parameters/Walk/blend_position", move_input)
        animation_tree.set("parameters/Idle/blend_position", move_input)

额外优化提示

  • 若希望角色Idle时保持最后一次移动的方向,可以去掉update_animation_parameters里move_input != Vector2.ZERO的判断,让Idle状态的方向始终跟随最新输入。
  • 动画树的blend_position只需要方向向量,传入未缩放的velocity或归一化后的向量效果一致,可根据需求选择。

内容的提问来源于stack exchange,提问作者Davide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 23:05:20