【问题标题】:Unity animation not completingUnity动画未完成
【发布时间】:2022-02-07 02:23:54
【问题描述】:

我是统一和游戏开发的初学者。我正在尝试制作一个统一的平台游戏,我想在玩家向右或向左移动时播放动画,但问题是 isMoving 变量出于某种原因是真正的动画正在播放片刻(不到一秒)然后再次变为假,并且正在播放空闲动画。

我的动画师(所有过渡都禁用退出时间):

我的运动脚本:

public class PlayerMovement : MonoBehaviour
{

    private bool canPlayerJump;
    private float horizontalInput;
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        

        if (Input.GetKeyDown(KeyCode.Space) && canPlayerJump)
        {
            GetComponent<Rigidbody>().AddForce(Vector3.up * 5, ForceMode.VelocityChange);
        }

        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.A))
        {
            animator.SetBool("isMoving", true);
        } else
        {
            animator.SetBool("isMoving", false);
        }

        horizontalInput = Input.GetAxis("Horizontal");

    }

    private void FixedUpdate()
    {
        GetComponent<Rigidbody>().velocity = new Vector3(horizontalInput * 5, GetComponent<Rigidbody>().velocity.y, 0);
    }

    private void OnCollisionEnter(Collision collision)
    {
       canPlayerJump = true;
       Debug.Log(collision.gameObject.name);
    }

    private void OnCollisionExit(Collision collision)
    {
        canPlayerJump = false;
    }

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您使用更新循环功能,这意味着此代码将在每一帧统一执行。类似于 FixedUpdate、LateUpdate 等... 你可以在这里阅读更多:HERE 一旦你按下你的 IF 语句将被执行,因为你正在监听 BTN DOWN 事件。这意味着当您按下实际按钮下一帧时,只会在一个帧中返回 TRUE,它将返回 false。你的动画就会停止。

    1. 尝试将 bool 改为 Trigger
    2. 或者以其他方式处理 bool var 状态。

    【讨论】:

      【解决方案2】:

      Input.GetKeyDown

      仅在按钮按下的一帧中为真。

      在用户开始按下由 key KeyCode 枚举参数标识的键的帧期间返回 true。


      您宁愿使用Input.GetKey,只要按键保持按下状态,它就会保持真实。

      当用户按住由 key KeyCode 枚举参数标识的键时返回 true


      或者,为了避免冗余访问,您也可以简单地使用

      horizontalInput = Input.GetAxis("Horizontal");
      
      animator.SetBool("isMoving", !Mathf.Approximately(horizontalInput, 0));
      

      作为一般说明:缓存组件引用,不要一遍又一遍地使用GetComponent

      [SerializeField] private Rigidbody _rigidbody;
      
      private void Awake ()
      {
          if(!_rigodbody) _rigidbody = GetComponent<Rigidbody>();
      }
      

      然后以后到处使用_rigidbdoy而不是GetComponent&lt;Rigidbody&gt;()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-10
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-16
        相关资源
        最近更新 更多