【发布时间】:2021-08-13 11:14:37
【问题描述】:
我正在制作第一人称游戏,但我无法为我的角色设置动画。我有正确的动画。我需要找到一种方法让游戏检测玩家何时刚刚降落在地面上,以便我可以播放“着陆”动画。问题是到目前为止我想到的唯一方法是使用协程。但是协程在启动时会完全冻结我的整个应用程序。我怀疑这是因为半空中的行为每帧启动一次协程。这是脚本:
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());
}
}
}
【问题讨论】:
标签: c# unity3d animation animator