如何在更新功能中使用WaitForSeconds不断旋转 - Unity

时间:2015-08-06 22:43:44

标签: c# unity3d

我正在寻找帮助,在Unity中更新功能延迟。

我在下面创建了这样的内容。立方体移动旋转一次然后等待>旋转一次>等等....

还有我的问题。如何让立方体不断旋转一段时间而不是一次。例如:等待2秒,持续旋转5秒,等待2秒,旋转....

我想到了替换

ForCube.transform.Rotate (10, 10, 10);

通过旋转动画。但我想用transform.Rotate创建它。有没有选择这样做?

using UnityEngine;
using System.Collections;

public class Ruch : MonoBehaviour
{
    public float speed = 5;
    public GameObject ForCube;
    bool work = true;

    // Use this for initializat
    void Start ()
    {
        ForCube = GameObject.Find ("Cube");
        Debug.Log (ForCube);
    }

    // Update is called once per frame
    void Update ()
    {
        if (work) {
            StartCoroutine (WaitSome ());
        }
    }

    private IEnumerator WaitSome ()
    {
        work = false;
        yield return new WaitForSeconds (3f);
        ForCube.transform.Rotate (10, 10, 10);
        work = true;
    }
}

3 个答案:

答案 0 :(得分:0)

目前看来我正在使用一个可以正常工作的StartCoroutine,但如果您想要更多地控制何时旋转以及何时停止,您可以使用Time.deltaTime The time in seconds it took to complete the last frame (Read Only). http://docs.unity3d.com/ScriptReference/Time-deltaTime.html

所以基本上你有一个名为Rotate的float变量,可以说是10f

然后在更新功能

void Update ()
{
    if(Rotate > 0)
    {
        Rotate -= Time.deltaTime;
        ForCube.transform.Rotate (10, 10, 10);
    }
}

然后当Rotate等于0时它会停止,但是你可以使用你的工作bool启动一个新的计时器。

一个很重要的想法是使用Time.deltaTime,如果你不使用它,你只需使用int或任何变量类型,定时器将根据玩家游戏的FPS而有所不同。 / p>

如果您需要更多帮助,请告诉我们。)

答案 1 :(得分:0)

您可以直接在更新函数中执行此操作,而不是使用协同程序,如下所示:

[SerializeField]
private float timeToWait;  //In seconds
[SerializeField]  
private float timeToRotate;  //In seconds

private float timer = 0;
private bool waiting = true;   //Set this to false if you want to rotate first, wait later

void Update() 
{
    if(!waiting) RotateYourObjectALittleBit();  //Call your own function or do whatever you want 

    timer += Time.deltaTime;

    if(timer >= timeToWait && waiting) {
        waiting = false;
        timer = 0;
    }
    else if(timer >= timeToRotate && !waiting) {
        waiting = true;
        timer = 0;
    }
}

此代码未经测试,如果您需要进一步说明或者它不起作用,请告诉我。

答案 2 :(得分:0)

感谢大家快速解答并帮助解决我的问题。我真的很感激。

我创造了这样的东西:

版本1.0
当空格键向下时,立方体开始旋转RotateTime,此后Timer重置为开始值(RotationTime),你可以再次点击按钮进行旋转。

using UnityEngine;
using System.Collections;
public class Ruch : MonoBehaviour
{
public GameObject ForCube;

public float RotateTime = 5;
public float Timer = 0;
private bool Rotate = false;

// Use this for initializat
void Start ()
{
    Timer = RotateTime;
    ForCube = GameObject.Find ("Cube");
    Debug.Log (ForCube);
}

// Update is called once per frame
void Update ()
{
    //Start Rotating When Press Space Key
    if (Input.GetKeyDown (KeyCode.Space)) Rotate = true;
    else if (!(Input.GetKeyDown (KeyCode.Space))&&Timer <=0) Rotate = false;

    RotateForSec (ref Timer);
}
//Function to Rotate for X sec
void RotateForSec(ref float sec)
{
    if (Rotate && sec > 0) {
        Debug.Log (Time.time);
        ForCube.transform.Rotate (10, 10, 10);
        sec -= Time.deltaTime;
    } 
    //Reset Rotating Time after rotating 
    if (!Rotate && sec <= 0) Timer = RotateTime;
}
}

版本2.0
立方体旋转持续5秒钟,然后自动无需按键,等待一段时间后重新开始旋转。一次又一次......

public GameObject ForCube;

public float RotateTime = 5;
public float Timer = 0;
public float PauseTime = 0;

private bool Pause = false;
private bool Rotate = true;

// Use this for initializat
void Start ()
{
    Timer = RotateTime;
    PauseTime = RotateTime;
    ForCube = GameObject.Find ("Cube");
    Debug.Log (ForCube);
}

// Update is called once per frame
void Update ()
{
    //Start Rotating When Press Space Key
    if (Rotate)
        Pause = false;
    else if (!Rotate) {
        Pause = true;
    }

    if (!Pause)
        RotateForSec (ref Timer);
        else RotatePause ();

}
//Function to pause PauseTime sec
void RotatePause()
{
    if (PauseTime > 0) {
        PauseTime -= Time.deltaTime;
    } else {
        Pause = false;
        Rotate = true;
        PauseTime = RotateTime;
    }

}
//Function to Rotate for X sec
void RotateForSec(ref float sec)
{
    if (Rotate && sec > 0) {
        Debug.Log (Time.time);
        ForCube.transform.Rotate (10, 10, 10);
        sec -= Time.deltaTime;

    } else Rotate = false;
    //Reset Rotating Time after rotating 
    if (!Rotate && sec <= 0) Timer = RotateTime;
}
}

它的工作但你对此有何看法,它是正确完成还是不好的方式?