如何在 C# 中等待 3 秒然后将 bool 设置为 true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16929805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:09:23  来源:igfitidea点击:

How can I wait for 3 seconds and then set a bool to true, in C#?

c#timerunity3d

提问by Froob

My script/game/thing make a gameobject move to the right and when I click dance (a button I created) it stops. Then when the counter (I may not need a counter but I want to wait 3 seconds) reaches like 3 (once you click on dance the counter starts) my gameobject is suppose to continue on going to the right.

我的脚本/游戏/事物使游戏对象向右移动,当我单击跳舞(我创建的按钮)时,它会停止。然后当计数器(我可能不需要计数器但我想等待 3 秒)达到 3 时(一旦你点击跳舞计数器开始),我的游戏对象应该继续向右移动。

If you can correct the code that would be cool. If you can correct it and explain to me what i did wrong it would be even more awesome. I just started learning C# on Unity.

如果你能更正代码那会很酷。如果你能纠正它并向我解释我做错了什么,那就更棒了。我刚开始在 Unity 上学习 C#。

using System;
using UnityEngine;
using System.Collections;

public class HeroMouvement : MonoBehaviour
{
    public bool trigger = true;
    public int counter = 0;
    public bool timer = false;

    // Use this for initialization

    void Start()
    {
    }

    // Update is called once per frame

    void Update()
    {  //timer becomes true so i can inc the counter

        if (timer == true)
        {
            counter++;
        }

        if (counter >= 3)
        {
            MoveHero();//goes to the function moveHero
        }

        if (trigger == true)
            transform.Translate(Vector3.right * Time.deltaTime); //This moves the GameObject to the right
    }

    //The button you click to dance 
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 50, 50), "Dance"))
        {
            trigger = false;
            timer = true;//now that the timer is set a true once you click it,The uptade should see that its true and start the counter then the counter once it reaches 3 it goes to the MoveHero function      
        }
    }

    void MoveHero()
    {  //Set the trigger at true so the gameobject can move to the right,the timer is at false and then the counter is reseted at 0.
        trigger = true;
        timer = false;
        counter = 0;
    }
}

回答by Andrew Ngo

First make counter a float. Then change counter++;to counter += Time.deltaTime. Update() is called for every frame so counter will be 3 on the third frame. Time.deltaTime gives you the time between this frame and the previous frame. Summing it up acts like a timer.

首先使计数器成为浮点数。然后更改counter++;counter += Time.deltaTime. Update() 为每一帧调用,因此第三帧的计数器将为 3。Time.deltaTime 为您提供此帧与前一帧之间的时间。总结起来就像一个计时器。

回答by VhsPiceros

if you need only wait, you can use sleep method of Thread

如果只需要等待,可以使用Thread的sleep方法

System.Threading.Thread.Sleep(3000);

回答by Jan Thom?

You could do it quite easily with coroutines:

你可以用协程很容易地做到这一点:

void Update()
{
    if (trigger == true)
        transform.Translate(Vector3.right * Time.deltaTime); //This moves the GameObject to the right
}

void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 50, 50), "Dance"))
        {  
           StartCoroutine(DoTheDance());
        }
    }


 public IEnumerator DoTheDance() {
    trigger = false;
    yield return new WaitForSeconds(3f); // waits 3 seconds
    trigger = true; // will make the update method pick up 
 }

See https://docs.unity3d.com/Manual/Coroutines.htmlfor more information about Coroutines and how to use them. They are pretty neat when trying to do a timed series of events.

有关协程及其使用方法的更多信息,请参阅https://docs.unity3d.com/Manual/Coroutines.html。在尝试进行一系列定时事件时,它们非常整洁。

回答by Basil Mariano

I prefer using StartCoroutine here is the link: http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

我更喜欢使用 StartCoroutine 这里是链接:http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

ex:

前任:

   void Foo () { StartCoroutine (Begin ()); }

   IEnumerator Begin ()
    {
        yield return new WaitForSeconds (3);

         // Code here will be executed after 3 secs
        //Do stuff here
    }

回答by Alan Sturtivant

I would use this if multi-threading:

如果是多线程,我会使用它:

    DateTime a = DateTime.Now;
    DateTime b = DateTime.Now.AddSeconds(2);

    while (a < b)
    {
        a = DateTime.Now;
    }

    bool = x;

回答by vicman4

I think that the easiest way is using Invoke:

我认为最简单的方法是使用 Invoke:

Unity3D Invoke

Unity3D 调用

if (timer == true) Invoke("MoveHero", 3);