- Home /
The question is answered, right answer was accepted
Dice Value
Hello there,
I have code for calculating dice value... they using dot product to get the value, i want to know about the code. if anyone know about dot product pls explain me or anyone know how to calculate the dice value tell me the logic. Here is the code.
using UnityEngine;
using System.Collections;
public class DiceValue : MonoBehaviour
{
public int side=0;
int CalcSideUp()
{
float dotFwd = Vector3.Dot(transform.forward, Vector3.up);
if (dotFwd > 0.99f) return 7;
if (dotFwd < -0.99f) return 4;
float dotRight = Vector3.Dot(transform.right, Vector3.up);
if (dotRight > 0.99f) return 8;
if (dotRight < -0.99f) return 3;
float dotUp = Vector3.Dot(transform.up, Vector3.up);
if (dotUp > 0.99f) return 5;
if (dotUp < -0.99f) return 2;
return 0;
}
void Update()
{
int side = CalcSideUp();
if (side > 0)
{
Debug.Log("Side = " + side);
}
}
}
-Prasanna.
Answer by robertbu · Feb 11, 2014 at 06:27 AM
This code is using the dot product in a very specific way. What they are doing only works if all the vectors involved are normalized (i.e. of unit length). All the ones they are use are normalized. When two normalized vectors are pointing in the same direction, their dot product value will be 1.0 (give or take a bit due to floating point imprecision). If two vectors are pointing in opposite directions, then dot product will be -1.
The other thing going on here is the value of each side of the cube. Their cube has the following values:
front - 7
back - 4
right - 8
left - 3
top - 5
bottom - 2
So take a look at lines 9 - 11. First the forward vector is compared with world up. If they are the same (i.e. dotFwd > 0.99f), then you know the forward side is facing up. If the value is near -1, then you know the back side is facing up.
Lines 13-15 text left/right.
Lines 17-19 test top bottom.
In the unlikely event the cube is on the edge (i.e. it got hung up on something), the function returns 0.
hi robertu, its working perfectly the cube without textures.. but i have a model with texture, so the problem is this code is not mixed with that texture.. basically am a designer. so i dont know about coding part. -Prasanna
If u know how to do this in another way. plz tell me. i want the code in c#..
-Prasanna
Bring your textured cube into unity. Set the rotation to 0.0. In Unity the front is the size fazing positive 'z'. So in the code above adjust the values for your cube. I've listed all the values and how they match to the sides, so you should be able to see what lines to change. For example, if your front side has the value of 2, then change the '7' to '2' in the code above. If your bottom is '5' then change the '2' in the code above to '5'.
Hi robertu, i think i made mistake with my animated object, i created my object starting rotation is 0,0,45.
Follow this Question
Related Questions
Efficient way to instantiate a sprite that plays an animation then destroys itself? (3d space) 1 Answer
How do I control animation with a value? 1 Answer
Character Wont Stop Moving 1 Answer
Triggering animation on click when a key is collected 1 Answer
How do I check if an Animation is playing in an animator 0 Answers