Search Unity

Code help

Discussion in 'Game Design' started by 2DNED, Dec 9, 2022.

  1. 2DNED

    2DNED

    Joined:
    Jan 17, 2016
    Posts:
    1
    Hi guys, im trying to put together a simple mini game
    12 Coins, match 3 you win.

    So far i watched a YouTube clip showing how to make a Coin Flip but if you keep pressing it it keeps flipping.
    What i want is a Random Selection and how to only Flip Once.

    Here is the code i have

    Code (CSharp):
    1. public class CoinScript : MonoBehaviour
    2. {
    3.     SpriteRenderer spriteRenderer;
    4.     public Sprite[] sides;
    5.     int flipCount = 1;
    6.  
    7.     private void OnMouseDown()
    8.     {
    9.         StartCoroutine(WaitPlease(0.0001f, 1.0f));
    10.     }
    11.  
    12.     IEnumerator WaitPlease(float duration, float size)
    13.     {
    14.         while(size > 0.1)
    15.         {
    16.             size = size - 0.07f;
    17.             transform.localScale = new Vector3(1, size, 1);
    18.             yield return new WaitForSeconds(duration);
    19.         }
    20.         spriteRenderer.sprite = sides[flipCount % 2];
    21.         while (size < 0.99)
    22.         {
    23.             size = size + 0.07f;
    24.             transform.localScale = new Vector3(1, size, size);
    25.             yield return new WaitForSeconds(duration);
    26.         }
    27.  
    28.         flipCount++;
    29.     }
    30.  
    31.     private void Awake()
    32.     {
    33.         spriteRenderer = GetComponent<SpriteRenderer>();
    34.     }
    35. }

    Here is a Sample Picture of what im doing



    Thanks in Advance.