1
\$\begingroup\$

I have a project where I flip a card after 5 seconds, then I want to start a countdown timer of x seconds. After x seconds the card flips again.

I found a good example for the timer here, but that works inside a Update() method. My problem comes from the fact that the rotation of the card is made also inside the Update() method.

If I put the timer inside the Update() method, as soon as the Update() method is called the timer starts, and I don't want it to start it as soon as I call the Update() method, but when I want to start ip, wich is 60 seconds after I flip the card.

If I put the timer inside a method other than Update() then the timer start but never decrease.

 using UnityEngine;
 using System.Collections;
 public class Juego1Carta2 : MonoBehaviour
 {
     public float velocidadDeRotacion = 100.0f;
     public Sprite cartaBase;
     public Sprite cartaDelantera;
     private bool carta2Activada = false;
     private bool cartaDeLado = false;
     float tiempoDeJuego = 2;
     IEnumerator Esperar()
     {
         yield return new WaitForSeconds(5);
         transform.position = new Vector3(Screen.width / 2, Screen.height / 2 +GetComponent<SpriteRenderer>().bounds.size.y, 0);
         carta2Activada = true;
         Regresa();
     }
void Regresa()
{
    Update();
}
// Use this for initialization
void Start ()
{
    StartCoroutine (Esperar());
}
// Update is called once per frame
void Update ()
{
    if (carta2Activada)
    {
        transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
        if (transform.eulerAngles.y >= 90)
        {
            SpriteRenderer Carta = GetComponent<SpriteRenderer>();
            if (Carta.sprite == cartaBase)
            {
                Carta.sprite = cartaDelantera;
                cartaDeLado = true;
                carta2Activada = false;
            }
            else
            {
                Carta.sprite = cartaBase;
                cartaDeLado = true;
                carta2Activada = false;
            }
            tiempoDeJuego = 60;
        }
    }
    tiempoDeJuego -= Time.deltaTime;
    if (tiempoDeJuego <= 0)
    {
        print("Fin del tiempo");
    }
    if (cartaDeLado)
    {
        transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
        if (transform.eulerAngles.y >= 180)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            cartaDeLado = false;
            carta2Activada = false;
            return;
        }
     }
   }
}

Is there a way to rotate the card and make a countdown timer at the same time?

I'm sorry, maybe this is a very obvious question but after 3 consecutive days working on this, I'm more than burnout...

\$\endgroup\$
1
\$\begingroup\$

You're using Update incorrectly. Update is called automatically every frame by Unity, on every component attached to every object (you can see this with the default comment that still exists in your code // Update is called once per frame). You don't need to call Update manually.

I see you're aware of having conditionals in the Update loop. What's wrong with having a if(timerStarted) conditional statement and putting all the timer code inside that? Then when you want to start your timer, just set timerStarted to true.

You can use use the coroutine like you are now, or simply have a different timer that sets timerStarted to true. Either one, will be activated in the Start() method, and 5 seconds later, timerStarted will be set to true, and will start being activated inside the Update loop.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.