How can I work out the orientation of my rotatable cube via script?
I have a simple cube object, and the player can move this around the world. Movements are rigid/locked, as opposed to physics-based, so the cube will always have a flush face on the ground surface and be oriented with 90 degree snapping.
I am having trouble solving how I can determine the current state of my cube. Sure, I know the cube's quaternion, but I struggle to reliably translate this to, for example, knowing in my component script that "the left face is currently facing down, and is oriented 90 degrees".
My current solution works, via raycasting onto the cube (each face has a different hit collider), but I feel this is overkill to compensate for a nicer solution which presumably should be more efficient than raycasting.
Any advice is much appreciated.
The orientation of the cube may be going down the wrong path, it isn't necessarily what I need, rather, I would like to know which face I would land on following any forward/back/left/right movements from the current orientation.
Eg. "the Left
face is currently facing down, so moving forward would roll the cube onto it's Back
face, moving left would roll the cube onto it's Up
face"
Answer by rmassanet · Jul 19, 2016 at 03:09 PM
A quick and dirty solution would be to calculate the angle distance from the cube's up
vector to all canonical vectors: Vector3.up
, Vector3.left
, etc... The canonical vector with the shortest distance to the up
vector would tell you how the cube is rotated.
Of course, that makes sense! Thank you.
To be honest, I wasn't aware that each GO had it's own orientation accessible (`transform.up`, etc.), but this was exactly what I was after. I was overcomplicating it trying to establish rotation, but this works ideally.
Can I get an example of what this might look like in code, I'm new to Unity and I'm not sure how to go about accomplishing this.
It's ok, got it to work. Thanks for the answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084951im_/https://answers.unity.com/themes/thub/images/avi.jpg)