Hello, I am currently making a 2D unity project as a major grade for a game program and design class. Our instructions were to make a 2D board game in unity. Right now, I am trying to script player movement based on dice rolls, but I am still new and I don’t really know how to script dice rolls by themselves, let alone use them for movement. I would appreciate any help on scripting this, and if anyone wants to give some tips, advice, or help for more steps later on I would appreciate that too.
Decompose the task into subtasks — there is no sufficient explanation of what you want and how it should work.
Try to learn the brand-new Game Development Pathway by Unity:
The simplest code to “roll” a dice is via UnityEngine.Random:
// Max is exclusive for integers, hence 7 and not 6 as upper bound
var diceRoll = Random.Range(1, 7);
Debug.Log("You rolled a: " + diceRoll);
NOTE: Everything you learn about software engineering and game development will be done by you and will be done one step at a time.
A common misconception is that reading about stuff and asking a few questions will suffice to turn around and do it successfully.
I urge you to hurry and get started right now, perhaps with a button that calls the code identified by @CodeSmile above and prints it to the Debug.Log().
If that is too big a chunk, break it down further and work on one thing at a time.
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
Step #2 is particularly critical when learning.
If you are unwilling or unable to do Step #2, just ask someone else to do the whole game for you.
Imphenzia: How Did I Learn To Make Games:
I like making a struct for dice and I use a similar structure to D&D. This lets me handle groups of dice e.g. 5D20 is rolling 5 x 20 sided dice. The nice thing about rolling multiple dice is that it creates a bell curve. You can also roll for different types of rolls e.g. advantage (max roll) or disadvantage (min roll). You can also override the operators to allow for expressions e.g. r2d20 + attack. If I’m making things like attack skills I like to use scriptable objects for dice so I can reuse them in formulas. The following is just a quick demo I put together and hasn’t been tested.
public enum DiceType
{
Sum,
Advantage, // Max dice number
Disadvantage // Min dice number
}
public readonly struct Dice
{
// 2D20 => amount = 2 dice, side = 20 sided dice
public readonly int amount;
public readonly int sides;
public readonly DiceType diceType;
public Dice(int amount, int sides, DiceType diceType = DiceType.Sum)
{
this.amount = amount;
this.sides = sides;
this.diceType = diceType;
}
private int RollSingleDice()
{
// (inclusive, exclusive) e.g d20, Range(1, 21)
return Random.Range(1, sides + 1);
}
public int Roll()
{
switch (diceType)
{
case DiceType.Sum:
{
var total = 0;
for (int i = 0; i < amount; i++)
{
total += RollSingleDice();
}
return total;
}
case DiceType.Advantage:
{
var max = 1;
for (int i = 0; i < amount; i++)
{
var roll = RollSingleDice();
max = Mathf.Max(max, roll);
}
return max;
}
case DiceType.Disadvantage:
{
var min = amount;
for (int i = 0; i < amount; i++)
{
var roll = RollSingleDice();
min = Mathf.Min(min, roll);
}
return min;
}
default:
throw new System.Exception("???");
}
}
// Overload operators e.g. Dice + int, int + Dice, Dice + Dice. TODO: other operators.
public static int operator +(Dice d, int amount) => d.Roll() + amount;
public static int operator +(int amount, Dice d) => amount + d.Roll();
public static int operator +(Dice d1, Dice d2) => d1.Roll() + d2.Roll();
}
public class DiceTest : MonoBehaviour
{
void Start()
{
var attack = 5;
var r2d20 = new Dice(2, 20);
var adv5d20 = new Dice(5, 20, DiceType.Advantage);
var dis5d20 = new Dice(5, 20, DiceType.Disadvantage);
var testRoll = r2d20.Roll();
var testAddDamage = r2d20 + attack;
var testAdvantage = adv5d20.Roll();
var testDisadvantage = dis5d20.Roll();
Debug.LogError($"testRoll:{testRoll}, testAddDamage:{testAddDamage}, testAdvantage:{testAdvantage}, testDisadvantage:{testDisadvantage}");
}
}