Getting dice number

Hello guys, i’m still newbie in unity,
i want ask you how to get number(value) from a dice(i make the dice from the box shape), should i use Random.Range? if like that, how to synchronize the value and the dice,
Thankyou, sorry for my bad english,

Yes, probably random range is the way to go for picking a number/value, but for actually animating something, you will have to use animations to match each “roll” of the dice to land on the random number (if you have a “dice” 3d gameobject or 2d sprite, anyway).

For six sided, it wouldn’t be that hard to make 6 animations of rolling to land to each side, then play those animations when it actually is time to show the result.

thanks, thats help me a lot, so first, get the value from random.range(), and after that call the animation based on the number we get, so basicly each valu/number has its own animation.
Thanks

1 Like

Yep, good luck!

Alternately, use the rotation of the cube to figure out which face is on top. That could be extended to other die shapes, in case you ever wanted to do polyhedral dice. Then you can use the physics engine to make every roll unique.

It might also be worth considering whether to just roll the dice, then determine the number after they land and use a particle effect or something to label the top face of the die.

  • Roll dice
  • Generate result
  • Wait to settle
  • Play fireworks over the die
  • Stick texture with result on top of die

When the fireworks are over, the result is just magically there, and instead of the die needing to have all the faces arranged properly and animated… you just have textured shapes.

Just brainstorming. It seems to me like manually animating rolls with the desired result would feel less satisfying than having unique physics-driven rolls.

Random.Range is fine. Here is different approach though.

public int getRandomDiceNumber ()
    {
        int DiceNumber = System.Environment.TickCount % 6;
        DiceNumber += 1;

        return DiceNumber;
    }

Not as much as you would think. Physics doesn’t always look as interesting one might think, even physically rolling a real dice in real life, doesn’t look all that interesting if you look at it. Creating your own animations (either directly via animation or procedural movement) typically looks way more interesting. Kind of the same way that the movie car crashes are more interesting than ones in real life.

I built few dice games in the past, and my approach was to generate the number(s) first and build the animations procedurally based on that, separating the display and logic. This affords two benefits:

  1. Often you want control over the results, AI, for example, that may adjust the results in favor or against the player depending on the game design, and
  2. Celebratory moments. As as an example, one of the games I made was a variation on Yahtzee, and if the player rolled and the results were a 5 match, it would do a much more dramatic roll with speed ramp at the end and camera zoom. If you calculate the results separate from a physics simulation, you can tailor the animation to the event.