[英]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.