# How to detect whether a transform has rotated left or right

I have a game object in my scene, and I want to know how can I calculate whether this game object rotating to the right or to the left.

For example :

if(check whether the transform rotated to the right)
{
print("rotate to right");
}
else if(check whether the transform rotated to the left)
{
print("rotate to left");
}
else
{
print("no rotation");
}


I'd recommend using the properties of the cross product to check this, rather than relying on Euler angles.

Given a vector a & b, the cross product will point along the rotation axis that swings a toward b. Since Unity uses left-handed coordinates, you can take your left hand in a "thumbs-up" gesture and turn it so you fingers curl in the direction of rotation. Your thumb points along the direction of the cross product.

For example, here's how we'd detect rightward (positive) and leftward (negative) "yaw" rotation about the world's Y+ axis:

public enum RotationDirection {
NoRotation,
Leftward,
Rightward
}

Vector3 oldForward
RotationDirection rotationDirection;

void Start() {
oldForward = transform.forward;
}

void Update() {
Vector3 cross = Vector3.Cross(oldForward, forward);

if (cross.y > 0f) }
rotationDirection = RotationDirection.Rightward;
} else if (cross.y < 0f) {
rotationDirection = RotationDirection.Leftward;
} else {
rotationDirection = RotationDirection.NoRotation;
}

oldForward = transform.forward;
}

• yes it is better way , thankyou very much for your help. @DMGregory – iman_sh Apr 23 '19 at 20:22

Do you have a rigidbody on it? Then you can check the angularVelocity field. If not, the best you can do is cache the previous orientation, then compare it to the current one. First let's define "right" and "left". To me, that means "clockwise about the z axis" and "counter clockwise about the z axis". What we want then is to do is this:

Quaternion oldRotation;
Quaternion currentRotation;
float zRotation = (Quaternion.Inverse(oldRotation) * currentRotation).eulerAngles.z;
if (z > 0) // we turned right
if (z < 0) // we turned left
if (z == 0) // we did not turn


Something like that should work. But if you have a rigidbody, do just use that.

• It helps me but with some edits , Thanx – iman_sh Apr 23 '19 at 20:14
• Great! Feel free to tell me the edits, then I can fix the answer for anyone looking in the future – gjh33 Apr 24 '19 at 13:35
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.44376763701438904, "perplexity": 2176.5289092926005}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-05/segments/1579250601615.66/warc/CC-MAIN-20200121044233-20200121073233-00026.warc.gz"}