[英]How to constantly rotating with WaitForSeconds in update function - Unity
我正在尋求有關延遲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;
}
}
目前,在我看來,您正在使用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
因此,基本上,您自己有一個名為Rotate的float
變量,可以說10f
然后在您的Update函數中
void Update ()
{
if(Rotate > 0)
{
Rotate -= Time.deltaTime;
ForCube.transform.Rotate (10, 10, 10);
}
}
然后,當Rotate等於0時,它將停止,但是您可以使用工作布爾來啟動新計時器。
一個重要的想法是使用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
當按下空格鍵時,多維數據集開始旋轉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;
}
}
版本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;
}
}
它可以正常工作,但是您對此有何看法,是做得正確還是不好?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.