简体   繁体   中英

Rotation and timer at the same time on Unity3D

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...

inside this if statement use the time that you set the timer as to execute the code after 60 seconds, something like this if i understand your question right

if (cartaDeLado)
    {
        if (tiempoDeJuego <= 0){ // dont execute any of the code unless 60 
                                 // seconds is up
        transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
        if (transform.eulerAngles.y >= 180)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            cartaDeLado = false;
            carta2Activada = false;
            return;
        }
        }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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