- Home /

**closed**Jan 14, 2018 at 12:37 AM by Nobozarb for the following reason:

The question is answered, right answer was accepted

# Find the difference between the Z rotation of two GameObjects

I have two 2D GameObjects, which I will call A and B. I want to be able to detect if the difference between A.transform.localEulerAngles.z and B.transform.localEulerAngles.z is less than 30 degrees via script. I tried finding the absolute difference between the rotations and using Mathf.DeltaAngle but for some reason those don't appear to work. Should those methods work and I am just implementing them incorrectly? Keep in mind that I want the difference between euler angles and not an axis of a quaternion.

**Answer** by Nobozarb
·
Jan 14, 2018 at 12:37 AM

I figured it out, by using Quaternion.Angle for anyone else who has this problem:

```
if (Quaternion.Angle (transform.rotation, B.transform.rotation) < 15) {
// do stuff!
}
```

**Answer** by Diukrone
·
Jan 13, 2018 at 11:42 PM

You can try a pseudo code here:

```
Transform positionA;
Transform positionB;
float anglesNow1;
float anglesNow2;
float anglesLimit;
float anglesSpeed;
float anglesDesired1;
float anglesDesired2;
float anglesCalc;
//Checking Angle 1
if (Input.GetAxis("Horizontal") > 0)
{
anglesDesired1 += anglesCalc;
}
if (Input.GetAxis("Horizontal") < 0)
{
anglesDesired1 -= anglesCalc;
}
if (Input.GetAxis("Horizontal") == 0) //You can disable this check for fixed positions!
{
anglesDesired1 = anglesCalc;
}
anglesNow1 = Mathf.SmoothDamp(anglesNow1, anglesDesired1, ref anglesSpeed, anglesLimit);
//Checking Angle 2
if (Input.GetAxis("Vertical") > 0)
{
anglesDesired2 += anglesCalc;
}
if (Input.GetAxis("vertical") < 0)
{
anglesDesired2 -= anglesCalc;
}
if (Input.GetAxis("vertical") == 0) //You can disable this check for fixed positions!
{
anglesDesired2 = anglesCalc;
}
anglesNow2 = Mathf.SmoothDamp(anglesNow2, anglesDesired2, ref anglesSpeed, anglesLimit);
//Now checking the real angles that inputs are on this moment.
If(anglesNow1 >= 31 && anglesNow2 >= 31) {
Debug.Log("Angles 1 and 2 are not synchronized, but both in high angles");
}
If(anglesNow1 == 30 && anglesNow2 == 30) {
Debug.Log("Angles 1 and 2 are synchronized, both in the desired angle");
}
If(anglesNow1 == 30 && anglesNow2 != 30) {
Debug.Log("Angles 1 are synchronized, but angle 2 are not!");
}
If(anglesNow1 != 30 && anglesNow2 == 30) {
Debug.Log("Angles 2 are synchronized, but angle 1 are not!");
}
If(anglesNow1 <= 29 && anglesNow2 <= 29) {
Debug.Log("Angles 1 and 2 are not synchronized, but both in low angles");
}
I use SmoothClamp to keep some axis value without to reach till the limit. If i use Lerp i cant control it till reach the maximum angle. When i stop to give input signals, the values will return to zero. You can disable if want fixed positions. Let me know if you fixed your issue! I hope it could helps you! See you, @Nobozarb!
```

Based on my understanding, this rotates two gameobjects to the same rotation. I wanted to check if two gameobjects were close to the same rotation. Sorry if the wording in my question was confusing.

I have updated my question to make it clearer.

No problem my friend! I am happy that you could fix your problem ^^

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

2d rotation doesn't work. 0 Answers

Clamp rotation of sprite? 0 Answers

Proper way to rotate an object? 1 Answer

Mathf.SmoothDamp freeking out? 0 Answers

[2D] Rotations become tighter the further left they are? 1 Answer