Search Unity

Rotation and timer at the same time on Unity3D

Discussion in 'Scripting' started by skyx26, Mar 2, 2015.

  1. skyx26

    skyx26

    Joined:
    Dec 30, 2014
    Posts:
    16
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Juego1Carta2 : MonoBehaviour
    4. {
    5.      public float velocidadDeRotacion = 100.0f;
    6.      public Sprite cartaBase;
    7.      public Sprite cartaDelantera;
    8.      private bool carta2Activada = false;
    9.      private bool cartaDeLado = false;
    10.      float tiempoDeJuego = 2;
    11.      IEnumerator Esperar()
    12.      {
    13.          yield return new WaitForSeconds(5);
    14.          transform.position = new Vector3(Screen.width / 2, Screen.height / 2 +GetComponent<SpriteRenderer>().bounds.size.y, 0);
    15.          carta2Activada = true;
    16.          Regresa();
    17.      }
    18. void Regresa()
    19. {
    20.     Update();
    21. }
    22. // Use this for initialization
    23. void Start ()
    24. {
    25.     StartCoroutine (Esperar());
    26. }
    27. // Update is called once per frame
    28. void Update ()
    29. {
    30.     if (carta2Activada)
    31.     {
    32.         transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
    33.         if (transform.eulerAngles.y >= 90)
    34.         {
    35.             SpriteRenderer Carta = GetComponent<SpriteRenderer>();
    36.             if (Carta.sprite == cartaBase)
    37.             {
    38.                 Carta.sprite = cartaDelantera;
    39.                 cartaDeLado = true;
    40.                 carta2Activada = false;
    41.             }
    42.             else
    43.             {
    44.                 Carta.sprite = cartaBase;
    45.                 cartaDeLado = true;
    46.                 carta2Activada = false;
    47.             }
    48.             tiempoDeJuego = 60;
    49.         }
    50.     }
    51.     tiempoDeJuego -= Time.deltaTime;
    52.     if (tiempoDeJuego <= 0)
    53.     {
    54.         print("Fin del tiempo");
    55.     }
    56.     if (cartaDeLado)
    57.     {
    58.         transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
    59.         if (transform.eulerAngles.y >= 180)
    60.         {
    61.             transform.eulerAngles = new Vector3(0, 0, 0);
    62.             cartaDeLado = false;
    63.             carta2Activada = false;
    64.             return;
    65.         }
    66.      }
    67.    }
    68. }
    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...
     
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    First, create a function outside of update that:
    rotates card
    starts a timer

    have a second function that:
    rotates the card back

    Now, use this https://gist.github.com/Valryon/9915075 which with a quick glance I could tell was awesome and exactly what you need.

    first, call your first function that flips the card and starts the timer...

    then using the script you find on the web page do this:


    // Advanced creation: you can launch it later and stop it

    //--------------------------------------------

    Timer t = new Timer(duration, false, () =>

    {

    //... call your second function that rotates the card back here

    });

    I haven't tested that code but it looks good :> should be simple for you now.