2D dice rolling
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;
}
}
Comment
Best Answer
Answer by TBruce · Jun 03, 2016 at 09:43 PM
You have a lot of variables that are never being set but you are using them. I have made some small mods to your code that hopefully will put you on track
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DiceRoll : MonoBehaviour
{
public Sprite[] Dice;
public Text rollText;
public int rolls;
public int rollValue;
public Text totalText;
public int total;
public int[] totalValue;
private bool isRolling;
private float totalTime;
private float intervalTime;
private int currrentDie;
private bool dieRolled;
private int currentTotal;
Image die;
void Start()
{
rolls = 0;
total = 0;
// moved this here because there is no need to continually get the same object
die = GameObject.Find("DieImage").GetComponent<Image>();
UpdateRolls();
Init();
}
private void Init()
{
totalTime = 0.0f;
intervalTime = 0.0f;
currrentDie = 0;
dieRolled = false;
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);
// removed the parameter because AddTotal() is called from wone place with same parameter
AddTotal();
}
}
}
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();
}
// removed the parameter because AddTotal() is called from wone place with same parameter
public void AddTotal()
{
// value is the true amount of the die face (currrentDie is 0 base so add 1)
int value = currrentDie + 1;
totalValue[currrentDie] += value; // add value to totalValue of current die
total += value; // add value to total
UpdateRolls();
}
void UpdateRolls()
{
rollText.text = "Rolls: " + rolls;
totalText.text = "Total: " + rolls;
}
}