- Home /
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 ^^
Follow this Question
Related Questions
Looking at target in 2D 1 Answer
how to rotate 2d obj on android 1 Answer
Animation in Unity editor prevents rotation in script 1 Answer
rotate 2d circle on z axis ? 1 Answer
Rotation Limit 2 Answers