- 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.

### People who like this

**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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

Clamp rotation of sprite? 0 Answers

2d rotation doesn't work. 0 Answers

Get an eular angle between 0 and 360 after calculations 2 Answers

Mathf.SmoothDamp freeking out? 0 Answers

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