【问题标题】:How can I wait for 3 seconds and then set a bool to true, in C#?如何在 C# 中等待 3 秒然后将 bool 设置为 true?
【发布时间】:2013-06-05 00:46:03
【问题描述】:

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

如果您可以更正代码,那就太好了。 如果你能纠正它并向我解释我做错了什么,那就更棒了。我刚开始在 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;
    }
}

【问题讨论】:

  • 你需要在空白处放轻松...
  • 是的,本可以有 1/5 的行!
  • 必须以klocs支付:)

标签: c# timer unity3d


【解决方案1】:

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

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 
 }

请参阅https://docs.unity3d.com/Manual/Coroutines.html 了解更多关于协程以及如何使用它们的信息。在尝试执行一系列定时事件时,它们非常整洁。

【讨论】:

【解决方案2】:

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

Unity3D Invoke

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

【讨论】:

    【解决方案3】:

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

    例如:

       void Foo () { StartCoroutine (Begin ()); }
    
       IEnumerator Begin ()
        {
            yield return new WaitForSeconds (3);
    
             // Code here will be executed after 3 secs
            //Do stuff here
        }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        如果是多线程我会使用这个:

            DateTime a = DateTime.Now;
            DateTime b = DateTime.Now.AddSeconds(2);
        
            while (a < b)
            {
                a = DateTime.Now;
            }
        
            bool = x;
        

        【讨论】:

          【解决方案6】:

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

          System.Threading.Thread.Sleep(3000);
          

          【讨论】:

          • 这将冻结更新循环。您不应该使用线程睡眠在更新循环中等待。
          • 让线程休眠并不是解决这个问题的好办法。 @JanThomä 是正确的。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-16
          • 1970-01-01
          • 2019-09-11
          • 2023-03-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多