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

Unity Animator参数满足条件时无法触发状态切换问题解决

Animator 布尔参数触发动画过渡失效问题

问题现象

实现动画状态从idle到attack的切换逻辑时,使用布尔类型参数作为过渡触发条件:代码中已正确给参数赋值,Animator面板可观测到参数已被置为true,但状态过渡始终不触发;进入运行模式后只要对Animator控制器做任意修改,过渡就会突然正常生效。

初始实现代码

public class Wraith : MonoBehaviour
{
    [SerializeField] GameObject projectile;
    [SerializeField] Vector3 projectilePosition;

    AttackerSpawner myLaneSpawner;
    Animator animator;

    void Start()
    {
        SetLaneSpawner();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        if (HasAttakerInLane())
        {
            animator.SetBool("shouldAttack", true);
        }
        else
        {
            animator.SetBool("shouldAttack", false);
        }

    }

相关配置

  • Animator控制器结构:Animator控制器结构
  • 过渡参数配置:过渡参数配置

修复方案

移除过渡的Exit Time设置,让过渡仅需满足布尔参数条件即可触发,同时调整代码逻辑,确保idle动画播放指定时长后再设置触发参数,修改后状态过渡可正常运行。
修复后的代码逻辑如下:

void Update()
{
    if (HasAttackerInLane() && animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.5f)
    {
        animator.SetBool("shouldAttack", true);
    }
    else
    {
        animator.SetBool("shouldAttack", false);
    }

}

内容的提问来源于stack exchange,提问作者meisam s.b.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 14:06:29