简体   繁体   English

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

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

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

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.html for more information about Coroutines and how to use them.有关协程及其使用方法的更多信息,请参阅https://docs.unity3d.com/Manual/Coroutines.html They are pretty neat when trying to do a timed series of events.在尝试进行一系列定时事件时,它们非常整洁。

I think that the easiest way is using Invoke:我认为最简单的方法是使用 Invoke:

Unity3D Invoke Unity3D 调用

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

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
    }

First make counter a float.首先使计数器成为浮点数。 Then change counter++;然后改变counter++; to counter += Time.deltaTime . counter += Time.deltaTime Update() is called for every frame so counter will be 3 on the third frame. Update() 为每一帧调用,因此第三帧的计数器将为 3。 Time.deltaTime gives you the time between this frame and the previous frame. Time.deltaTime 为您提供此帧与前一帧之间的时间。 Summing it up acts like a timer.总结起来就像一个计时器。

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;

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

System.Threading.Thread.Sleep(3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM