[英]Code not starting/stopping animation
从逻辑上讲,这段代码对我来说很好,但是当我运行动画未激活或停用的代码时,它无法正常工作。 我把qwerty int放在那里用于测试目的。
void WaitingForPipe () {
qwerty = 1;
PipeEntry.GetComponent <Animator>().enabled=true;
Wait ();
qwerty = 2;
//yield return new WaitForSeconds(2);
PipeEntry.GetComponent<Animator>().enabled=false;
qwerty = 3;
//GameObject.Find("FPSController").GetComponent("FirstPersonController").enabled=true;
}
IEnumerator Wait()
{
//This is a coroutine
//Debug.Log("Start Wait() function. The time is: "+Time.time);
//Debug.Log( "Float duration = "+duration);
yield return new WaitForSeconds(2); //Wait
//Debug.Log("End Wait() function and the time is: "+Time.time);
}
C#中的协同程序不是通过调用它们运行的方法,只能在UnityScript中运行。 要在C#中启动Coroutine,必须调用StartCoroutine(YourCoroutine());
在MonoBehaviour
实例上。
如果代码如下所示,您的代码将起作用:
IEnumerator WaitingForPipe()
{
PipeEntry.GetComponent <Animator>().enabled=true;
yield return new WaitForSeconds(2);
PipeEntry.GetComponent<Animator>().enabled=false;
}
你必须使用StartCoroutine(WaitingForPipe());
启动Coroutine StartCoroutine(WaitingForPipe());
来自某个地方。
您可以在官方文档中阅读有关Coroutines的更多信息: https ://docs.unity3d.com/Manual/Coroutines.html
看起来你想等待2秒才能运行Wait ();
。 如果那是真的那么你必须yield
你的Wait ();
功能。
更改
Wait ();
至
yield return Wait();
并且你必须将你的WaitingForPipe
函数更改为一个协同程序函数,以便你可以在其中产生。 void WaitingForPipe ()
应该是IEnumerator WaitingForPipe ()
代码无法启动/停止动画
您的问题中没有启动或停止动画的代码,您不必也不应该启用/禁用Animator以播放或停止它。 那是错的。 删除PipeEntry.GetComponent <Animator>().enabled=true/false
代码 。
这是播放/停止动画的正确方法:
玩 :
string stateName = "Run";
Animator anim = GetComponent<Animator>();
anim.Play(stateName);
停止 :
anim.StopPlayback();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.