In: Computer Science
In Unity with C#, I need to implement a dice simulator in my character generator(D&D). I have 6 sides of UI images(2D). With my code, I am able to roll and get the number in my console text(randomly).
But, I need help for rolling 5 dices, not the 1 dice. Also, I need to add all the three highest rolls with 5 dices and get a result in my display.
Ex) 1. Players must roll 5d6 and add the three highest rolls to compute each of the six Abilities. Note make all modifiers default to +2.
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
public class diceRoller : MonoBehaviour
{
public int numberDice;
public int sidesDice;
public int modifierDice;
public InputField numDice;
public InputField sidesDice;
public InputField diceModifier;
public Text outputText;
System.Random ranDice = new System.Random();
void Start()
{
numDice = numDice.GetComponent<InputField>();
sidesDice = sidesDice.GetComponent<InputField>();
diceModifier = diceModifier.GetComponent<InputField>();
outputText = outputText.GetComponent<Text>();
}
public void RollDice()
{
numberDice = Convert.ToInt32(numDice.text);
sidesDice = Convert.ToInt32(sidesDice.text);
modifierDice = Convert.ToInt32(diceModifier);
int counter = numberDice;
while (counter > 0)
{
int randomNum = randDice.Next(1, sidesDice);
outputText.text = outputText.text + Environment.NewLine + "You Rolled 1D" + sidesDice + "+" + modifierDice + " for " + randomNum + ". Your result is " + (randomNum + modifierDice);
counter = (counter - 1);
}
}
}