Advertisement
Guest User

Untitled

a guest
May 13th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class Dice : MonoBehaviour {
  5.  
  6.     private Sprite[] diceSides;
  7.     private SpriteRenderer rend;
  8.     private int whosTurn = 1;
  9.     private bool coroutineAllowed = true;
  10.  
  11.     // Use this for initialization
  12.     private void Start () {
  13.         rend = GetComponent<SpriteRenderer>();
  14.         diceSides = Resources.LoadAll<Sprite>("DiceSides/");
  15.         rend.sprite = diceSides[5];
  16.     }
  17.  
  18.     private void OnMouseDown()
  19.     {
  20.         if (!GameControl.gameOver && coroutineAllowed)
  21.             StartCoroutine("RollTheDice");
  22.     }
  23.  
  24.     private IEnumerator RollTheDice()
  25.     {
  26.         coroutineAllowed = false;
  27.         int randomDiceSide = 0;
  28.         for (int i = 0; i <= 20; i++)
  29.         {
  30.             randomDiceSide = Random.Range(0, 6);
  31.             rend.sprite = diceSides[randomDiceSide];
  32.             yield return new WaitForSeconds(0.05f);
  33.         }
  34.  
  35.         GameControl.diceSideThrown = randomDiceSide + 1;
  36.         if (whosTurn == 1)
  37.         {
  38.             GameControl.MovePlayer(1);
  39.         } else if (whosTurn == -1)
  40.         {
  41.             GameControl.MovePlayer(2);
  42.         }
  43.          if(randomDiceSide != 5) whosTurn *= -1;
  44.         coroutineAllowed = true;
  45.    
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement