Virtual Dice Roller
I’ve been doing assorted small projects that are tasks building up to a tactical RPG. One of the latest of these is a nifty dice roller I wanted to show off. Here’s a gif of a couple 6-sided dice being rolled, plus debug logging of the roll’s result:

To start with I obviously needed a 3d model of a 6-sided die. That’s pretty easy to model and I could’ve whipped it up fairly quickly, but why bother when there are plenty available to download. I just downloaded this one and cleaned it up a bit in Blender. Basically I reduced the level of the Bevel modifier to reduce the polygon count, and then I exported it as FBX.
The roll was basically just Unity’s built-in physics. I just applied an upward force and some random torque, with both set to Impulse mode.
Really the trickiest part of this code was detecting the number on top of the dice. I’ve seen guides online telling you to make a collider on each side to tell which side was colliding with the ground, but that approach seems like way overkill to me. Instead, I simply did a dot product between Vector3.up and the three coordinate vectors of the cube’s transform (transform.forward, transform.up, and transform.right) in order to determine which side of the cube was facing up.
This dice rolling effect looks pretty good, but the major downside is that the animation of the dice needs to play out before I can determine their value. In other words, I can’t do a “skip animations” option in my game.
Well, I found this reddit thread about deterministic dice rolling someone came up with. He actually comes up with the random values before the dice roll, and set the animation to show those values. I’m going to try implementing his explanation in my dice roller; wish me luck!
Discussion