C# Unity,用 Animator 编写一个有限状态机,但是当条件停止时我有一个奇怪的循环

问题描述

请帮助说明为什么在条件停止后这种循环很奇怪,当“机器人”完成他的放置时,现在每个“法力”只需向下移动一次,它就会不断循环,然后就停止,就好像它仍处于该状态一样。

法力增量为 0.5,移动 1 次消耗 1 法力

    override public void OnStateUpdate(Animator animator,AnimatorStateInfo stateInfo,int layerIndex)
{
    animator.SetBool("HasTarget",false);
    GameObject go = GameObject.Find("GM");
    if (GameObject.Find("GM").GetComponent<Placement>().Current_mana >= 1)
    {
    
        go.GetComponent<StateMachineHelper>().SelectUnit.transform.position = 
            go.GetComponent<StateMachineHelper>().SelectUnit.transform.position - new Vector3(0,1);
        go.GetComponent<Placement>().ClickUnit = null;
        go.GetComponent<Placement>().distance = false;
        go.GetComponent<Placement>().Current_mana--;
    }else//(GameObject.Find("GM").GetComponent<Placement>().Current_mana <= 0.5)
    {

        go.GetComponent<Placement>().ChangeTurn();

        animator.SetBool("IsWaiting",true);
    }



}

解决方法

好的,所以我的愚蠢问题的答案是...我将其更改为 OnStateEnter 而不是更新版本,并且只是对 FSM 中的前一个节点进行了新的转换以循环它直到我的法力耗尽。