- Home /
Dice Animation and Face Determination
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#?
Could you mark an answer? You've got 2 pages of them.
private var ismoving : boolean = false; private var startY : float = 0; var cubeSpeed : float; var cubeSize: int;
function Update () {
if (Input.Get$$anonymous$$eyDown("up") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(0, -cubeSize/2 , cubeSize/2);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.right, 90.0f,cubeSpeed));
}
if (Input.Get$$anonymous$$eyDown("down") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(0, -cubeSize/2, -cubeSize/2);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.right, 90.0f,cubeSpeed));
}
if (Input.Get$$anonymous$$eyDown("left") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(-cubeSize/2, -cubeSize/2, 0);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.forward, 90.0,cubeSpeed));
}
if (Input.Get$$anonymous$$eyDown("right") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(cubeSize/2, -cubeSize/2, 0);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.forward, 90.0f,cubeSpeed));
}
}
function DoRoll (aPoint, aAxis, aAngle, aDuration) {
var tSteps = $$anonymous$$athf.Ceil(aDuration * 30.0); var tAngle = aAngle / tSteps; var pos : Vector3; // declare variable to fix the y position
// Rotate the cube by the point, axis and angle for (var i = 1; i <= tSteps; i++) { transform.RotateAround (aPoint, aAxis, tAngle); yield WaitForSeconds(0.033333); }
// move the targetpoint to the center of the cube transform.Find("targetpoint").position = transform.position;
// $$anonymous$$ake sure the y position is correct pos = transform.position; pos.y = startY; transform.position = pos;
// $$anonymous$$ake sure the angles are snaping to 90 degrees.
var vec = transform.eulerAngles; vec.x = $$anonymous$$athf.Round(vec.x / 90) 90; vec.y = $$anonymous$$athf.Round(vec.y / 90) 90; vec.z = $$anonymous$$athf.Round(vec.z / 90) * 90; transform.eulerAngles = vec;
// The cube is stoped ismoving = false;
}
Answer by Jaap Kreijkamp · Dec 02, 2009 at 12:57 AM
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);
}
}
Answer by Ricardo · Dec 02, 2009 at 02:33 AM
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.
Answer by h2hjastermereel · Dec 03, 2009 at 03:26 PM
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..
Answer by Avtcoco · Mar 11, 2011 at 10:44 PM
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?
Answer by marjan · May 31, 2013 at 12:31 PM
late answer, but you can also add 6 empty gameobjects, parented under your model. adjust their positions to the middle of each face. After Rolling you check which one has the highest y position in world space. This works also with rolling objects exept a pyramid. You could store them in an array at special indexes, this it is easy to find an according up value. (any number from 0 to 5 for example.)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
When dragging a Door open using RIGIDBODYSCRIPT Plays sound. 1 Answer
Determine Area using code 1 Answer
How to keep physics consistent? 1 Answer
constant force 1 Answer