简体   繁体   English

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

[英]How to constantly rotating with WaitForSeconds in update function - Unity

I am looking for a help with make a delay in Unity in Update function. 我正在寻求有关延迟Unity更新功能的帮助。

I created something like this below. 我在下面创建了类似的东西。 The cube is moving rotates once and then is waiting > rotates once > waiting .... 多维数据集正在移动旋转一次,然后等待>旋转一次>等待...。

And there is my question. 还有我的问题。 How i can make cube rotates constantly for some time instead of once. 我如何使立方体不断旋转一段时间而不是一次。 For Example: Wait 2sec, rotating constantly 5sec, Wait 2sec, rota.... 例如:等待2秒,持续旋转5秒,等待2秒,旋转...。

I thinked about replace 我考虑过更换

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

by rotating Animation. 通过旋转动画。 But I want create it with transform.Rotate. 但是我想用transform.Rotate创建它。 Is there any option to do this? 有什么选择吗?

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

At the moment it looks like to me you are using a StartCoroutine which will work fine, but if you want maybe a little more control over when to rotate and when to stop you can use the Time.deltaTime The time in seconds it took to complete the last frame (Read Only). 目前,在我看来,您正在使用StartCoroutine,它将很好地工作,但是如果您想对何时旋转和何时停止进行更多控制,则可以使用Time.deltaTime The time in seconds it took to complete the last frame (Read Only).所需The time in seconds it took to complete the last frame (Read Only). http://docs.unity3d.com/ScriptReference/Time-deltaTime.html http://docs.unity3d.com/ScriptReference/Time-deltaTime.html

So basically you have yourself a float variable called Rotate which is lets say 10f 因此,基本上,您自己有一个名为Rotate的float变量,可以说10f

Then inside of your Update function 然后在您的Update函数中

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

Then when Rotate is equal to 0 it will stop, but then you can use your work bool to start a new timer. 然后,当Rotate等于0时,它将停止,但是您可以使用工作布尔来启动新计时器。

One big think to take in is to use the Time.deltaTime, if you don't use this and you just use an int or whatever variable type the timer will differ depending on the FPS of the game for the player. 一个重要的想法是使用Time.deltaTime,如果您不使用它,而只是使用int或任何变量类型,则计时器会根据玩家的游戏FPS而有所不同。

Let me know if you need anymore help :) 如果您需要其他帮助,请告诉我:)

Instead of using coroutines, you can do it directly in the update function like this: 除了使用协程,您可以直接在更新函数中执行以下操作:

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

This code is untested, so please let me know if you require further clarification or if it doesn't work. 该代码未经测试,因此,如果您需要进一步的说明或不起作用,请告诉我。

Thanks everyone for fast Answer and help to solve my problem. 感谢大家的快速回答,并帮助解决我的问题。 I really appreciate that. 我真的很感激。

I created something like this: 我创建了这样的东西:

Version 1.0 版本1.0
When the space key is down the cube start rotating for RotateTime, after this the Timer reset to start value(RotationTime), and u can click again button for rotate. 当按下空格键时,多维数据集开始旋转RotateTime,此后计时器重置为起始值(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;
}
}

Version 2.0 版本2.0
The rotating of cube continues for 5 seconds and then automatically without pressing a key it wait some time and start over rotating. 立方体的旋转持续5秒钟,然后自动旋转而无需按任何键,它会等待一段时间并重新开始旋转。 Again again and again ... 一次又一次...

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

Its working but what you think about that, is it done correctly or it is a bad way? 它可以正常工作,但是您对此有何看法,是做得正确还是不好?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM