Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by JoaquinGuzman · Aug 30, 2015 at 09:12 PM · rotationrelativerelative rotation

How do I check a rotation relative to another object's rotation?

Hi! I need to check an object's rotation relative to another object's rotation. Just like the image, I need to know when the yellow ball has rotated outside the brown boundaries set by the blue ball. I have a problem since the blue ball also rotates and I don't know how to check it. To add some context, the blue ball are my character's legs which rotate as given by the arrow keys (up, down, left, right, up-left, down-right, etc), and the yellow ball is the upper body which rotates relative to the mouse, but I can't have the legs facing south and the upper body facing north, so I need to check rotations to be able to change the leg's animationalt text.

sprite0-strip2.png (16.4 kB)
Comment
Add comment · 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 itsharshdeep · Aug 31, 2015 at 09:07 AM 0
Share

I don't know the exact answer but I think vecto3.angel can work for you. Please check http://docs.unity3d.com/ScriptReference/Vector3.Angle.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mikilo · Aug 31, 2015 at 09:16 AM

Hello!

You might use this code to calcul some angles.

 public static class Utility
 {
     public static float        RelativeAngle(Vector3 fwd, Vector3 targetDir, Vector3 upDir)
     {
         var    angle = Vector3.Angle(fwd, targetDir);

         if (Utility.AngleDirection(fwd, targetDir, upDir) == -1)
             return -angle;
         else
             return angle;
     }
     
     public static float        RelativeAngle(Vector2 fwd, Vector2 targetDir, Vector3 upDir)
     {
         var    angle = Vector2.Angle(fwd, targetDir);

         if (Utility.AngleDirection(fwd, targetDir, upDir) == -1)
             return -angle;
         else
             return angle;
     }

     public static float        AngleDirection(Vector3 fwd, Vector3 targetDir, Vector3 up)
     {
         Vector3    perp = Vector3.Cross(fwd, targetDir);
         float    dir = Vector3.Dot(perp, up);

         if (dir > 0F)
             return 1F;
         else if (dir < 0F)
             return -1F;
         else
             return 0F;
     }

     public static float        AngleDirection(Vector2 fwd, Vector2 targetDir, Vector3 up)
     {
         Vector3    perp = Vector3.Cross(new Vector3(fwd.x, 0f, fwd.y),
                                      new Vector3(targetDir.x, 0f, targetDir.y));
         float dir = Vector3.Dot(perp, up);

         if (dir > 0F)
             return 1F;
         else if (dir < 0F)
             return -1F;
         else
             return 0F;
     }
 }


In your case, your might need this particular code:

     float angle = Utility.RelativeAngle(FORWARD_OF_YOUR_LEGS, FORWARDS_OF_YOUR_HEAD, Vector3.up);
Comment
Add comment · Show 4 · 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 JoaquinGuzman · Aug 31, 2015 at 09:34 AM 0
Share

I'm sorry, but I'm trying to implement your code to see if it works and I have no idea what "NGUtility" is and I can't find it anywhere. Forgive me for my noobiness... really.

avatar image Mikilo · Aug 31, 2015 at 09:50 AM 0
Share

Holy shit, sorry I replaced the class name only.. I edit it for you.

avatar image JoaquinGuzman · Aug 31, 2015 at 09:59 AM 1
Share

No problem, thank you very much, I was really stuck with this :D

avatar image Mikilo · Aug 31, 2015 at 10:01 AM 0
Share

You are welcome my friend, good luck.

As far as I remember, RelativeAngle will give you an angle from your front, -180 to 0 if it is in left side, and 0 to 180 if in the right side.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add rotation from an angle 1 Answer

How to Rotate Relative To World 1 Answer

Rotate an object such that it is theta degrees relative to another object 1 Answer

Reletive rotation problems. (C#) 2 Answers

'Rotating' a vector to have 1 axis = 0 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