- Home /
Detect the up axis when rolling a cube?
I've successfully roll a cube using the example here: http://unitynoobs.blogspot.com/2011/02/roll-cube.html
How can I determine which axis is going to be in the up position?
Answer by Pandantly · May 01, 2016 at 07:24 AM
This probably isn't the most efficient (or creative) solution, but here's an idea:
Imagine you've created a rubik's cube in unity, with it's staring position being with the white side up. You can go to the scene and take note of it's current rotation value. Then, turn the cube so the red side is up, and take note of it's current rotation value, continuing until you've finished all sides. Then, in your script, you can create a variable for each different rotation value. For instance:
/* Variables could be used to store the rotation value the cube had when a certain side was facing upwards */
Vector3 blueUp = new Vector3 (0, 0, -90);
Then, you could simply check each frame which variable the cube's current rotation value matches. If the cube's r-value is equal to blueUp, the blue side is facing upwards. If it's equal to whiteUp, the white side is facing upwards.
One could argue that, in a sense, this is "cheating," but it's an option. Hope this helps!
Answer by N1warhead · May 01, 2016 at 03:47 AM
Like predict what axis will be the up position after the dice is rolled?
Unfortunately this won't be easy as it depends how you're even doing this first. Like if you are using pure physics to role the dice, I highly doubt it will be possible to pre-determine the axis of which will be pointing up because every roll at least in real life will never be the same, the bounce, the force, etc. Now I'm sure it is possible if you really spend some time to make a big system that can accurately predict this stuff, however - there will not be any easy solution with a couple lines of code, unless you can figure out the math involved from the force, bounce, drag, rotational force, Angular Drag, etc.
Aside from that the only way i can see it even being possible is if you make a bunch of Animations that plays random animations, which wouldn't really be so great.
prediction isn't easy man, but predetermined stuff is. Now what you CAN DO is use some simple bools that will TELL you which one is angled Upwards.
Something like this.
private bool x;
private bool y;
private bool z;
private Transform tran;
private Rigidbody diceRigid;
void Start(){
tran = this.transform;
diceRigid = GetComponent<Rigidbody>();
}
void Update(){
//So we check if the X axis is point up and if the Dice is no longer moving.
if(tran.eulerAngles.x == 90 && diceRigid.velocity == Vector3.zero){
//Do the same for Y and Z as well.
//Do Code Here, x at 90 degrees = straight up.
x = true;
}
}
//So pretty much, if X axis = 90 degrees, then X is true. So therefore X is straight up.
Your answer
Follow this Question
Related Questions
OnMouseDown() cube face? 1 Answer
Object Problems with colliders? 1 Answer
Everytime ball goes to new plane it bounces 2 Answers
How can i stop a WASD Cube Player to move into nothing on a plattform? 2 Answers
moving a cube 2 Answers