2013-06-05 16 views
12

Komut dizim/oyun/şey sağa doğru bir hamle hareketi yapar ve ben dansı tıkladığımda (oluşturduğum bir düğme) durur. Daha sonra sayaç (bir sayaca ihtiyaç duymayabilirim ama 3 saniye beklemek istiyorum) 3'e ulaştığı zaman (sayacı dansa tıkladığınızda dans başladığında), gameobject’imin sağa gitmeye devam edeceği varsayılır.3 saniye bekleyip C# olarak bir boole doğru olarak nasıl bekleyebilirim?

Takılı olabilecek kodu düzeltebilirseniz. Eğer düzeltip bana neyi yanlış yaptığımı açıklarsanız daha da harika olur. Sadece C# 'yı Unity' de öğrenmeye başladım.

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

Sen boşluk kolay gitmek gerekir ... – paddy

+0

evet, çizgilerin 1/5 yaşadım olabilir! – ApolloSoftware

+0

klocs içinde ödenmelidir :) – ApolloSoftware

cevap

3

İlk önce bir yüzer kontrat yapın. Sonra counter++; değerini counter += Time.deltaTime olarak değiştirin. Her çerçeve için Update() çağrılır, böylece üçüncü karede sayaç 3 olacaktır. Time.deltaTime, bu kare ile önceki kare arasındaki süreyi size verir. Özetlemek bir zamanlayıcı gibi davranır. Yalnızca beklemek gerek olmadığını

+0

Teşekkürler bunu deneyin – Froob

-7

, sen Konu uyku yöntemini kullanabilirsiniz

System.Threading.Thread.Sleep(3000); 
+2

Bu, güncelleme döngüsünü donduracak. İplik uykusunu kullanarak güncelleme döngüsünde beklememelisiniz. –

+0

İş parçacığının uyku moduna geçmesi, bu sorun için iyi bir çözüm değildir. @ JanThomä doğru. – SlxS

+0

Bu kod ana parçayı dondurur. Bunu yapma. –

14

Sen değiş tokuş eden kavramlar ile bunu kolaylıkla yapabilirsiniz: nasıl

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 
} 

Eşyordamlar ve hakkında daha fazla bilgi için http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html bakın onları kullan. Zamanlanmış bir dizi etkinlik yapmaya çalışırken oldukça düzgünler.

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

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

    bool = x; 
+0

Unity 5: StartCoroutine (DoTheDance()) 'da çalışması için bunun gibi yazılacak başlangıç ​​coroutine satırına ihtiyacım vardı; – skribbz14

+1

Teşekkürler, kodu güncelledim. –

2

link yolu Invoke kullanıyor:

Unity3D Invoke

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

+0

Bu bağlantı soruyu yanıtlayabilirken, cevabın temel kısımlarını buraya dahil etmek ve referans için bağlantı sağlamak daha iyidir. Bağlantılı sayfa değiştiğinde yalnızca bağlantı yanıtları geçersiz olabilir. – Ghost

+0

Teşekkürler Hayalet! :) Sadece benim ans düzenledi. –

-2

Bu ise çoklu iş parçacığı kullanmak ben StartCoroutine burada kullanmayı tercih

+0

Kodunuzu girin. – gsamaras

+1

Bu iş parçacığı döndürecek ... alternatifler ara! – hooby3dfx