簡體   English   中英

代碼無法啟動/停止動畫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM