- Home /
Dice Rolling Detection
How would I create a system that rolls a dice, and detects what the side lands on? I COULD just use Raycasts or Vector3.Dot or something, but how would I do that with a four-sided dice? Eight-sided? Ten-sided? Twenty-sided? Is there a simple system for doing this? Is it possible to simply detect the side that is touching the floor?
If you need complete flexibility with die sizes, shapes, and number of faces, just check all faces for the normal closest in angle to Vector3.up once the die comes to rest. Using the Dot product this is very little calculation compared to, for example, having colliders on each face (likely more costly per set of calculations plus run each physics update).
Vector3.Dot(Vector3.up, side.normal); // Find the result closest to 1!
You need some meta-data for your sides in any case to define what number or result they represent, so just store the normal as well. It's probably best to pre-calculate the local space normal in Start() then transform Vector3.up to local space before calculating the dot products.
I'm not sure if this answers you fully so I'll leave it as a comment... are you also thinking how to deter$$anonymous$$e which faces are what value on the die etc?
Answer by tWayfarer · Nov 16, 2013 at 03:03 AM
You can get the collision's contact points, and from there find the direction to from the centre to the collision point. You'll have to figure out in advance the direction to each face, which there's probably an elegant way of doing which I am unaware of. Rigidbody physics could probably handle rolling the die itself, after an initial force applied. Then, just wait until it's stopped, use the contact points to find the face on the bottom, compare the angle and you've got the face. If you want the opposite face to the one on the bottom, just reverse the angle before comparing it.