Hello,
I need some help with a 2D dice rolling script. There is 1 die and when clicked on the images flash randomly and when it stops the number of rolls is displayed. This is working, But i would like to add a total of the dice as they are displayed by there face value. I have tried as it shows in the script but not sure how to get a list of numbers 1 to 6 to correspond to the images 1 through 6 as they are displayed. Please let me know if anyone has any ideas.
Thank you
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DiceRoll : MonoBehaviour
{
public Sprite[] Dice;
private bool isRolling;
private float totalTime;
private float intervalTime;
private int currrentDie;
private bool dieRolled;
private int currentTotal;
public Text rollText;
public int rolls;
public int rollValue;
public Text totalText;
public int total;
public int[] totalValue;
Image die;
void Start()
{
rolls = 0;
total = 0;
UpdateRolls();
Init();
}
private void Init()
{
totalTime = 0.0f;
intervalTime = 0.0f;
currrentDie = 0;
dieRolled = false;
die = GameObject.Find("DieImage").GetComponent<Image>();
die.sprite = Dice[currrentDie];
}
void Update()
{
if (isRolling)
{
intervalTime += Time.deltaTime;
totalTime += Time.deltaTime;
if (intervalTime >= 0.1f)
{
//change die & rotation
currrentDie = Random.Range(0, 6);
die.transform.Rotate(0, 0, Random.Range(0, 360));
//set image to selected die
die.sprite = Dice[currrentDie];
intervalTime -= 0.1f;
}
if (totalTime >= 2.00f)
{
isRolling = false;
dieRolled = true;
AddRolls(rollValue);
AddTotal(totalValue);
}
}
}
public void DieImage_Click()
{
if (!dieRolled)
{
isRolling = true;
}
if (!isRolling)
{
Init();
isRolling = true;
}
}
public void PanelTestButton_Click()
{
GameObject panel = GameObject.Find("DiePanel");
Animator animator = panel.GetComponent<Animator>();
animator.Play("DiePanelOpen");
}
public void PanelOKButton_Click()
{
GameObject panel = GameObject.Find("DiePanel");
Animator animator = panel.GetComponent<Animator>();
animator.Play("DiePanelClose");
rolls = 0;
total = 0;
UpdateRolls();
}
public void AddRolls(int newRollValue)
{
rolls += newRollValue;
UpdateRolls();
}
public void AddTotal(int[] newtotalValue)
{
total += newtotalValue[currentTotal];
UpdateRolls();
}
void UpdateRolls()
{
rollText.text = "Rolls: " + rolls;
totalText.text = "Total: " + rolls;
}
}