Unity:如何使update()函数等待几秒钟

时间:2019-08-02 12:39:33

标签: c# unity3d time

所以我试图让我的update()函数每次在玩家按下按钮“ C”时等待几秒钟,因为每次当玩家按下键“ c”时,它都会重置对象的旋转,而Im尝试让我的游戏等待几秒钟,因为它将使对象的一些小动画重置其旋转值。

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);
    }
}

2 个答案:

答案 0 :(得分:0)

您应该添加返回类型重置方法。例如

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

使用此功能时,需要使用startcoroutine方法。

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

答案 1 :(得分:0)

使用这个:

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");
    }
}