I’m currently trying to design a board game involving dice with Unity, but I’m a bit confused as to how I can determine which face of each die is up (as well as the location of the adjacent faces relative to it).
While I’m aware I could probably parse the rotational values, I’m concerned Unity’s returned values aren’t going to be very reliable.
For example, in the editor, I created a cube and changed the orientation by 90 degrees on each axis a few times to determine how the orientation change on each axis would affect the others. Afterwards, I briefly selected a different object and then switched back to the cube. Suddenly, the inspector panel shows the rotational values going from clean, 90 degree increments to a lengthy floating point value like “5.0009641e”.
Is there an option besides parsing the orientation values that might work for my needs?
The only other option I can currently think of is to create a set of child collider objects inside each die that would contain a central collider object bound by gravity that would eventually come to rest on the collider face at the bottom of the die. However, I’m worried such a system would quickly bog down Unity when employing more than a couple dice at a time.
Actually, this code fragment posted by freyr in that thread might be enough for what I need and could probably be adapted to identify the adjacent face position/values as well.
// The 3 low-end expressed in local space vectors.
// We don't need the last 3 as their direction
// vectors are simply the negative of the
// opposite side.
// (You will have to change the vectors if
// you have orientated the dice differently.)
var sides : Vector3[] =
[Vector3.up, // 1(+) or 6(-)
Vector3.right, // 2(+) or 5(-)
Vector3.forward]; // 3(+) or 4(-)
function WhichIsUp() {
var maxY = float.NegativeInfinity;
var result = -1;
for(var i = 0; i < 3; i++) {
// Transform the vector to world-space:
var worldSpace = transform.TransformDirection(sides[i]);
if(worldSpace.y > maxY) {
result = i+1; // index 0 is 1
maxY=worldSpace.y;
}
if (-worldSpace.y > maxY) { // also check opposite side
result = 6-i; // sum of opposite sides = 7
maxY=-worldSpace.y;
}
}
return result;
}
My javascript is a bit rusty still, but I think I understand the gist of it though.
Even better… you could know the face value with RaycastHit.textureCoord. This way you don’t have to create manually the array and this method should work with n-side dice.
Well, it seems that code segment posted by freyr does work, but it’s kind of tricky if you didn’t specifically design your die model for it ahead of time.
Here’s what I eventually came up with:
// sets the side definition variables.
// (these have been changed to the literal vector
// values for readability)
var sides : Vector3[] =
[Vector3(0, 1, 0), // 1(+) or 6(-)
Vector3(0, 0, 1), // 2(+) or 5(-)
Vector3(1, 0, 0)]; // 3(+) or 4(-)
function WhichIsUp() {
var maxY = float.NegativeInfinity;
var result = -1;
for(var i = 0; i < 3; i++) {
// Transform the vector to world-space:
var worldSpace = transform.TransformDirection(sides[i]);
if(worldSpace.y > maxY) {
result = i+1; // index 0 is 1
maxY=worldSpace.y;
}
if (-worldSpace.y > maxY) { // also check opposite side
result = 6-i; // sum of opposite sides = 7
maxY=-worldSpace.y;
}
}
return result;
}
// display the die value
function OnGUI () {
var thisSide = WhichIsUp() + "";
GUILayout.Label (thisSide);
}
Combined with this die model (175KB Unity-compatible Cheetah3D .jas file), the above behavior script displays the value of the die’s top-most face when the project is run. To test it, simply change the orientation of the die in the editor and run the project again to have it display the result.
For anyone having difficulty getting their own models working right with the above behavior script, here’s a couple of suggestions:
First, try adjusting the orientation of your model in your 3D modeling app so that the center of faces one and six are resting on the Y-axis. (When imported into unity, the model should be oriented with side one at the top.)
Next, if you keep getting the wrong result where the number at the bottom of the die is being displayed instead of the top, you may need to swap those sides of the die with each other. The fastest way to do this, is to scale your model by -100% in your 3D modeling app along the axis the faces are centered on. this effectively mirrors your model to the appropriate sides. (Just be sure to flip your object normals immediately after to avoid rendering issues later on.)