[英]How do I pause GameObject for a couple of seconds and unpause after a couple of seconds in unity3d
I need the gameobject
to pause on its own in my scene for 7f or 8f and un-pause at 2f on its own. 我需要gameobject
在场景中自行暂停7f或8f,并自行在2f处取消暂停。 The script I have is letting my pause by key. 我拥有的脚本是让我暂停输入。 Here is my script : 这是我的脚本:
{
sing UnityEngine; using System.Collections; public class star : MonoBehaviour { GameObject[] pauseObjects; void Start () { pauseObjects = GameObject.FindGameObjectsWithTag("Player"); } void pauseGameobject() { if() { start coroutine("wait"); } } public ienumenator wait() { time.timescale = 0; yield return new waitforsceonds(7); time.timesale = 1; } void pauseGameobject() { if() { start coroutine("wait"); } } public ienumenator wait() { time.timescale = 0; yield return new waitforsceonds(7); time.timesale = 1; }
} }
It's not too clear what you mean by pause on it's one, however I'm going to answer broadly, to try and help you. 暂时停顿一下是什么意思,目前还不清楚,不过我将广泛回答,以帮助您。
If you whant to pause a single game object externally you can deactivate it and activate it accordingly with this code: gameObject.SetActive(false);
如果您想从外部暂停单个游戏对象,则可以使用以下代码停用它并相应地激活它: gameObject.SetActive(false);
Instead if you want to pause the game object internally you can make a bool and in the update test wether or not it's true: 相反,如果您想在内部暂停游戏对象,则可以设置布尔值,无论是否进行更新测试,它都是正确的:
using UnityEngine;
using System.Collections;
bool update = false
public class ActiveObjects : MonoBehaviour
{
void Start ()
{
//Do stuff
}
void Update ()
{
if(update){
//Do stuff
}
//decide wether or not to pause the game object
}
}
If you want to pause the game you can set the Time.timeScale
to 0
, or just pause all game objects. 如果要暂停游戏,可以将Time.timeScale
设置为0
,或者只是暂停所有游戏对象。
Here you can find how to make a timer, all you need to do is count down a variable using timeLeft -= Time.deltaTime;
在这里,您可以找到如何制作计时器,您所要做的就是使用timeLeft -= Time.deltaTime;
倒数一个变量timeLeft -= Time.deltaTime;
. 。
Hope I helped you, 希望我能帮助你,
Alex 亚历克斯
Edit: Ok, here is the script, keep in mind I have no way to test it ;) 编辑:好的,这是脚本,请记住,我无法对其进行测试;)
using UnityEngine;
using System.Collections;
public class star : MonoBehaviour {
GameObject[] pauseObjects;
public float timer = 7;
float t = 0;
bool pause = false;
void Start () {
pauseObjects = GameObject.FindGameObjectsWithTag("Player");
t = timer;
}
void Update() {
if(pause){
if(t<0){
t=timer;
pause = false;
time.timescale = 1;
}else{
t -= Time.deltaTime;
time.timescale = 0;
}
}
}
You can use coroutines to insert delays in the update()
loop. 您可以使用协程在update()
循环中插入延迟。 Coroutines use generators, which "yield", rather than functions/methods which "return". 协程使用“屈服”的生成器,而不是“返回”的函数/方法。 What this allows for is code that operates asynchronously while still being written in a linear fashion. 这允许的是异步操作的代码,同时仍以线性方式编写。
The built in coroutine you're most likely looking for is WaitForSeconds . 您最可能需要的内置协程是WaitForSeconds 。 To start a coroutine you simply call StarCoroutine()
and pass in any method of type IEnumerator
. 要启动协程,您只需调用StarCoroutine()
并传入IEnumerator
类型的任何方法即可。 This method will yield
periodically. 此方法将定期yield
。 In the following example, WaitForSeconds(5)
will yield after 5 seconds. 在以下示例中, WaitForSeconds(5)
将在5秒后屈服。 Fractions of a second can also be used, represented by floats, for example 2.5 would be two and a half seconds. 也可以使用小数秒,以浮点数表示,例如2.5将是两秒半。
using UnityEngine;
using System.Collections;
public class WaitForSecondsExample : MonoBehaviour {
void Start() {
StartCoroutine(Example());
}
IEnumerator Example() {
Debug.Log(Time.time); // time before wait
yield return new WaitForSeconds(5);
Debug.Log(Time.time); // time after wait
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.