Help on how to script a working dice

Hi all, I’m working on an augmented reality monopoly game in Unity with markers, and a working dice is just what I need.

However, after many failed attempts I’m still unable to get some satisfying results.

====================================================

Here’s how I would like my dice to work:

  1. When a particular marker is covered then revealed under a camera the dice is triggered to roll.

  2. The dice has 6 sides. I need to find a random number script to generate even possibility between “One” to “Six”. Each with a 1/6 chance to appear.

  3. Then a 3D animation is triggered accordingly to the outcome of the dice.

=====================================================

So does any resourceful master have the solution to script out the above commands?
Please, I’m really stuck. :frowning:

You’d need six animations for each outcome of the dice. However a way to get past having six animations for each dice would be to have six dice and ONE animation that lands with the same side at the top each time. Each dice would be textured so that the top number would be different. Therefore you could just select a dice, roll it, and it would have the number you want on top. Either that, or have six individual animations you can select.

Generating a random number is not difficult, simply google for either javascript or C# examples of random number generation.

Activating a trigger should be simple enough, and there are plenty of examples.

A run through of your script might look like this:

Trigger activated.
Generate a random number between one and six.
Use that generated number to select dice animation.
Play selected dice animation.

What bit are you having trouble with specifically? Is it finding a working random number generator or creating the animations for the dice for each number?

Use the random function. In this case:

DiceVariable = Random.Range(1, 6)

Or one animation with 6 UVmaps.

…Or one animation that always lands in the same position, but put the dice as a child object.

Pick a random number 1-6 to determine what side will face up. Pick a random number 1-4 to decide how the dice will be oriented after landing. Or pick a random number 1-24 to do both at once.

You could also have multiple animations, or combinations of animations, to make the dice roll seem more natural and random. Maybe certain animations curves could be slightly tweaked at runtime. Making the rolls look natural without wasting resources on physics simulations is the most difficult part.

As much as they might appreciate your answer, the thread is almost 12 years old, surely they have moved on by now. Make sure to check that you’re not necro-ing old threads like this.

2 Likes

Since it’s already bumped, I answered a similar question on UnityAnswers some years back. It works with any number of sides, though you have to setup the “sides” manually. Though I’ve linked some scripts which would help with the setup. In essence you can give every side a normal vector and the side which is closest aligned to the world up vector, is the upper side.