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

### Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. **Please note, Unity Answers is now in read-only so we can prepare for the final data migration.**

For more information and updates, please read our full announcement thread in the Unity Forum.

### Follow this Question

### Related Questions

Clamp rotation of sprite? 0 Answers

Rotating x Degrees 1 Answer

2d rotation doesn't work. 0 Answers

Mathf.SmoothDamp freeking out? 0 Answers

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