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

本文关键字:旋转 WaitForSeconds Unity 更新 新功能 | 更新日期: 2023-09-27 18:06:41

我正在寻找一个帮助,使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;
    }
}

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

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

基本上我们有一个float变量叫做Rotate我们写上10f

然后在Update函数

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

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

一个重要的想法是使用Time.deltaTime,如果你不使用这个,你只是使用int或其他变量类型,计时器将根据玩家的游戏FPS而不同。

如果你还需要帮助,请告诉我。

不使用协程,您可以直接在更新函数中这样做:

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

这个代码是未经测试的,所以如果你需要进一步的澄清或如果它不工作,请告诉我。

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

我创建了如下内容:

1.0版
当空格键按下时,立方体开始旋转旋转时间,之后计时器重置为开始值(旋转时间),你可以再次点击旋转按钮。

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

它在工作,但是你怎么看,它是正确的还是一个坏的方法?