簡體   English   中英

Unity:如何讓 update() 函數等待幾秒鍾

[英]Unity: how to make the update() function wait a few seconds

所以我試圖讓我的 update() 函數在每次玩家按下按鈕“C”時等待幾秒鍾,因為每次當玩家按下鍵“c”時,它都會重置對象的旋轉,我試圖讓我的游戲等待幾秒鍾,因為它會使對象的一些小動畫重置他的旋轉值。

void Reset()
{
    Vector3 newRotation = 
    gameObject.transform.rotation.eulerAngles;

    if (Input.GetKeyDown(KeyCode.C))
    {

        x = newRotation.x;
        y = newRotation.y;
        z = newRotation.z;

        x = Mathf.Round(x);
        y = Mathf.Round(y);
        z = Mathf.Round(z);

        yield return new WaitForSeconds(1);

        print(x + " " + y + " " + z);

        for (; x >= 0; x--)
        {

            arotation.x = x;
            boxy.transform.Rotate(arotation.x, y, z);
            if (x == 0)
            {
                for (; y >= 0; y--)
                {
                    arotation.y = y;
                    boxy.transform.Rotate(arotation.x, arotation.y, z);

                    if (y == 0)
                    {
                        for (; z >= 0; z--)
                        {
                            arotation.z = z;
                            boxy.transform.Rotate(arotation.x, arotation.y, arotation.z);
                        }
                    }
                }
            }
        }
        print(x + " " + y + " " + z);
    }
}

您應該添加返回類型重置方法。 例如

IEnumerator Reset() {
   // your process
   yield return new WaitForSeconds(1);
  // continue process
} 

當你使用這個函數時,你需要使用 startcoroutine 方法。

void Update() {
     StartCoroutine("Reset");
}

用這個:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine("Reset");
    }

    // Update is called once per frame
    void Update()
    {
    }
    IEnumerator Reset()
    {
        //Put your code before waiting here

        yield return new WaitForSeconds(1);

        //Put code after waiting here

        //You can put more yield return new WaitForSeconds(1); in one coroutine

        StartCoroutine("Reset");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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