dice upperface detection

hello,

i want to calculate the upper face of dice; after googling i got the following code. this code doesnot work perfect, sometime it returns 0 value for upper face.
could someone help me to sort out the issue.
//function to calculate upper side
//currObject is the dice

function CalcSideUp(currObject : GameObject) {
var dotFwd : float = Vector3.Dot(currObject.transform.forward, Vector3.up);

if (dotFwd >= 0.99) return 5;
if (dotFwd <= -0.99) return 2;

var dotRight : float= Vector3.Dot(currObject.transform.right, Vector3.up);

if (dotRight >= 0.99) return 4;
if (dotRight <= -0.99) return 3;

var dotUp : float = Vector3.Dot(currObject.transform.up, Vector3.up);

if (dotUp >= 0.99) return 6;
if (dotUp <= -0.99) return 1;

return 0;
}
Thanks

There are several reasons why it could go wrong. Is the surface the dice fall on always facing world up? If it can be tilted, you should use the surface normal instead of Vector3.up.

Also, .99 is a bit tight. You should easily get away with .7 or something along those lines. That would give you a lot less chance of running into floating point inaccuracy problems.

Might be a bit of an odd way of looking at things but you could fire a ray from the faces to get which one is grounded (i.e facing DOWN) then use that “opposite numbers on a die add up to seven” trick to work out what face was up. i.e if 2 is down then the upper face would be 5, 1 would be 6, 3-4 etc