• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 14, 2018 at 12:37 AM by Nobozarb for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Nobozarb · Jan 13, 2018 at 10:15 PM · 2drotationeuleranglesmathf2d rotation

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.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Diukrone · Jan 13, 2018 at 11:43 PM 0
Share

Check my answer! I hope it helps you!

2 Replies

  • Sort: 
avatar image
Best Answer

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!
 }
Comment
Kshesho

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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!
 
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nobozarb · Jan 14, 2018 at 12:19 AM 0
Share

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.

avatar image Diukrone Nobozarb · Jan 14, 2018 at 12:49 AM 0
Share

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

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges