- Home /
Check an Objects Rotation
Hello,
I know there is a way to check the position of an object along x, y and z, but is it possible to check an objects rotation along x, y and z also? My intention for this is to rotate a cube by 90 degrees along the y axis and to check its rotation using an if statement to trigger an event. I have placed an example of what I would like to do below.
if(target is rotated at 90 along y-axis)
//do something
Answer by kolban · May 20, 2012 at 02:43 AM
An object can be rotated "around" various axis but isn't rotated "along" an axis (just a small nit). Given an object, you can access its Transform properties. One of the transform properties is a property called "rotation" which is the object's rotation defined as a "quaternion". A quaternion object has a property called "eulerAngles" which is a Vector3 representing the objects rotation around the x, y and z axis.
So ... given an object called MyObject ... the following can be used:
if (myObject.transform.rotation.eulerAngles.y == 90)
{
// Do something
}
There's also an eulerAngles property in Transform, transform.eulerAngles - it's the same as transform.rotation.eulerAngles
it should be noted that either transform.eulerAngles
, and transform.rotation.eulerAngles
is prone to being slightly inaccurate by sometimes as much as a full degree. so when testing against a eulerAngle
it is best to do so in a range of values (so for this example ((y>89)&&(y<91))
for an example of this create a controllable object, and only rotate it about a given axis. you should notice that the values of the other axis are changing as well (this is due to converting a euler angle to a quaternion to rotate the quaternion, and then converting the quaternion back to euler angles to display) the same will happen when you purely modify the quaternion by a fixed angle (unless you are doing so purely through the inspector then the math flow of the engine differs slightly, and is more precise).
thank you very much, this solved my problem. i was rotating an object by 90 degrees and then checked whether the object has a certain rotation. even though the rotation in the inspector was correct but the value in the code was different(i ws using eulerAngles) and adding this to the code solved my problem.
Thanks to all of you for your help, it provided me with all that I needed to get this listeners working and learn more ter$$anonymous$$ology as well. Also thanks $$anonymous$$evin_C for mentioning the margin for error as it possibly saved me a lot of possible script debugging.
@$$anonymous$$evin_C , thank you, been sitting with this issue for one hour now !
Answer by ShawnFeatherly · Sep 08, 2017 at 12:52 AM
While angle decreasing
Mathf.DeltaAngle() tells you the angular distance between two Euler axis. Saving the deltaAngle from the last frame enables checking if the delta is decreasing. This works great if something is continually rotating along a single axis at fast speeds (less than 180 degrees a frame).
public IEnumerator RotateTo90()
{
float deltaAngle = 360;
bool isAngleDecreasing;
do
{
float lastDeltaAngle = deltaAngle;
var amountToRotate = angularSpeed * Time.deltaTime;
centricTransform.Rotate(Vector3.up, amountToRotate);
deltaAngle = Mathf.Abs(Mathf.DeltaAngle(this.rotation.eulerAngles.y, 90));
isAngleDecreasing = deltaAngle < lastDeltaAngle;
yield return null;
} while (isAngleDecreasing);
}
Alternate Quaternion.Dot method
I couldn't get this method to work well. It is likely more performant. It may work for your scenario. Quaternion.Dot can be used to see how close two rotations are to one another. It returns 1 or -1 when the rotations coincide and 0 when they're opposite directions.
Quaternion targetAngle = Quaternion.Euler(0, 90, 0);
float precision = 0.9999f;
if (Mathf.Abs(Quaternion.Dot(this.transform.rotation, targetAngle)) > precision)
{
// do something
}
Your answer
Follow this Question
Related Questions
Positions to rotations of a regular GO 2 Answers
Relative Rotation 1 Answer
How to change an objects position and rotation inside a script? 0 Answers
Unable to set Rotation 1 Answer