科罗廷完全冻结Unity 2020.3



我正在制作一个第一人称游戏,在为我的角色设置动画时遇到了问题。我有合适的动画。我需要找到一种方法,让游戏检测到玩家何时刚刚落地,这样我就可以播放"落地"动画。问题是,到目前为止,我唯一想到的方法就是协同出游。但是协同程序在启动时会完全冻结我的整个应用程序。我怀疑这是因为在半空中的行为每帧启动一次协同飞行。这是脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimationHandler : MonoBehaviour
{
Animator animator;
IEnumerator LandDetect()  
{
while (!playermove.isGrounded)
{
animator.SetBool("Midair", true);
}
animator.SetTrigger("Land");
yield return null;
}
PlayerMove playermove;
void Start()
{
// These are the two most important components for this 
// script. I'll need PlayerMove for the mini-API
// that I have in there and I'll need the animator
// for obvious reasons.
playermove = GetComponent<PlayerMove>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
JumpHandler();
}
void JumpHandler()
{
if (!playermove.isGrounded)
{
if (playermove.doubleJumpOccur)
{
animator.SetTrigger("DoubleJump");
StartCoroutine(LandDetect());
}
else
{
animator.SetBool("Midair", true);
StartCoroutine(LandDetect());
}
}
else if (playermove.jumpOccur)
{
animator.SetTrigger("Jump");
StartCoroutine(LandDetect());
}
}

}

我怀疑应用程序没有离开

while (!playermove.isGrounded)
{
animator.SetBool("Midair", true);
}

直到你着陆。这个循环在同一帧中连续执行(这会冻结你的游戏(,因为你没有跳过帧。您需要将yield return 0(0,而不是null。null不会导致协同程序跳过任何帧(放在其中的某个位置,以便在下一帧中恢复。也可以在方法结束时删除yield return null,此时我不做任何

我认为它应该是这样的:

IEnumerator LandDetect()  
{
while (!playermove.isGrounded)
{
animator.SetBool("Midair", true);
yield return 0;
}
animator.SetTrigger("Land");
}

此外,我不知道在这里使用协同程序是否是最好的选择。我通常会使用一些触发/碰撞检测来处理这样的事情。

最新更新