# Dice Animation and Face Determination

 0 I want to roll a few dice models and be able to determine the face that is UP at the end of the roll on the various dice and use those face values in both JavaScript and/or C# scripts. First, how would you roll dice models in Unity and secondly, can someone give an example of determining which face is up on a die model in JavaScript and C#? more ▼ asked Dec 02 '09 at 12:47 AM Scott 1 57 ● 2 ● 2 ● 3 Ricardo 5.2k ● 20 ● 32 ● 96 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 A quick and dirty solution would be to calculate the Vector3.Dot product of the x, y, z axises of your cube against Vector3.up. If a value is (close to) 1 that side is up, if a value is (close to) -1, that side is down (so opposite side is up). Enough info to build your own code with or do you need a script? EDIT What the heck, here's a script (the side numbers need to be adjusted to how you've got your dice set up, for me, top = 6, bottom = 1, right side = 4, left side = 3, front side = 5, back side = 2. The code is in C# but shouldn't be hard to convert to Javascript. ``````using UnityEngine; using System.Collections; public class JDiceSideUp : MonoBehaviour { int CalcSideUp() { float dotFwd = Vector3.Dot(transform.forward, Vector3.up); if (dotFwd > 0.99f) return 5; if (dotFwd < -0.99f) return 2; float dotRight = Vector3.Dot(transform.right, Vector3.up); if (dotRight > 0.99f) return 4; if (dotRight < -0.99f) return 3; float dotUp = Vector3.Dot(transform.up, Vector3.up); if (dotUp > 0.99f) return 6; if (dotUp < -0.99f) return 1; return 0; } void Update() { int side = CalcSideUp(); if (side > 0) Debug.Log("Side = " + side); } } `````` more ▼ answered Dec 02 '09 at 12:57 AM Jaap Kreijkamp 6.4k ● 20 ● 26 ● 70 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 How you would roll dice would depend on if you're using a physics-based model, determining the result because of the dice bouncing around, or if you want to simulate the rolling to land on a specific number. If you're going for the physics model, you could do worse than to check out the source code for Rock'n'Roll dice, which I released under the Artistic License about 11 months ago. You can see a video of it here. more ▼ answered Dec 02 '09 at 02:33 AM Ricardo 5.2k ● 20 ● 32 ● 96 the link is down! Dec 16 '11 at 06:22 AM ina add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Working on a prototype which involves dice, I was having the same problem of getting the 'up' face of the rolled dice. I used the 'dot product' solution, which works pretty well. I'm now facing the extension of the problem: how to determinate the result of a die with n-faces? eg: D4 (with the resulting face at the bottom), D8, D10, etc. The configuration of each die is different, and I wonder if another approach would work. Some hints maybe: using different materials for each face? using properties on (mesh) faces? using a top-bottom raycast to the center of the die to get the top-most face. But might not work for non-symetric dice? Any suggestions? more ▼ answered Mar 11 '11 at 10:44 PM Avtcoco 2 ● 2 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Note that in the above code there is dead zone of 0.99. However unlikely if those values come up the dice value will be 0, which is likely not what you want, change some of the operators to be <= or >= etc.. more ▼ answered Dec 03 '09 at 03:26 PM h2hjastermereel 11 ● 1 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x5099
x1882
x575
x18

asked: Dec 02 '09 at 12:47 AM

Seen: 4427 times

Last Updated: Dec 16 '11 at 06:22 AM