【问题标题】:Animation gets stuck at the end in Unity Animator动画在 Unity Animator 中卡在最后
【发布时间】:2021-02-18 19:02:06
【问题描述】:

我不擅长 3D 动画,所以我有一个小问题。我正在用蝙蝠制作摆动动画。每个动画都可以正常工作,但是这里的问题是,当从空闲动画转到摆动动画时,它会卡住,我必须单击几次才能返回空闲状态。 here is the animation

Here is where the thing gets stuck

在这里你可以检查这两个 trainsition 的检查器:

from idle to swing from swing to idle

我正在尝试解决这个问题一段时间,但仍然不明白为什么会这样。 在这里您可以查看此代码。该脚本专门用于近战武器,但我认为它也适用于其他物品。代码中的所有内容都有效,除了这个动画部分。

using UnityEngine;

public class melee : MonoBehaviour
{
    public float dmg;
    public float range;
    
    private Animator anim;
    private bool attacking = false;
    
    public Camera cam;

    void Start(){
        anim = gameObject.GetComponent<Animator>();
    }



    void FixedUpdate(){
        AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
        if(Input.GetButtonDown("Fire1")){
            Attack();
            
        }else if(Input.GetButtonUp("Fire1")){
            anim.SetBool("attacking", false);
        }

    }

    void Attack(){
        RaycastHit hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range)){
            Debug.Log(hit.transform.name);

            enemyHealth enemy = hit.transform.GetComponent<enemyHealth>();
            
            if(enemy != null){
                enemy.TakeDmg(dmg);
            }

        }


        anim.SetBool("attacking", true);
    }
}

【问题讨论】:

    标签: c# unity3d game-engine


    【解决方案1】:

    我认为发生的情况是您的输入首先进入 if 并且永远不会进入第二个 if 或再次被第一个 if 覆盖。

    所以你总是将布尔值设为 TRUE。

    另外,我认为您不需要固定更新。定期更新应该会更好。

    【讨论】:

      【解决方案2】:

      FixedUpdate() 在一帧中被调用多次以保证最精确的物理计算。是的,Xentios 是怎么说的

      所以你总是把布尔值设为 TRUE。 另外我认为您不需要固定更新。定期更新应该会更好。

      您更改布尔值的速度过快。您可以在动画结束后将该标志设置为 FALSE。如果攻击没有击中任何人,也许你可以加快动画速度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-10
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多