Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MaeniAck · May 06, 2012 at 05:59 PM · directionangleanglesfacing

Angle between two objects' facing direction

Hello Everybody...

I'm currently working on a project which requires me to do something I just can't figure out.

  • need to know the angle between two objects facing directions. Let me explain; if you look down on two arrows, with the heads down the x-axis, rotated randomly, around the y-axis. Now I need to know the angle between the two arrowheads.*

So if the two arrows face away from each other I need an angle of 180 degrees, and If they face the same direction I need an angle of 0 degrees.

I tried everything from trigonometric functions to simple unity references, and nothing seems to work how I want it, I have a hunch the Vector3.Angle could work, but so far it doesn't.

Comment
Add comment
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

2 Replies

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

Answer by Piflik · May 06, 2012 at 06:08 PM

Vector3.Angle gives you the angle between two directions. Just put transform.forward of both objects in there, and it should give you the angle you need.

It has one limitation, though, it doesn't distinguish between CW and CCW. I wrote a function for that:

 function angleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
     var perp: Vector3 = Vector3.Cross(fwd, targetDir);
     var dir: float = Vector3.Dot(perp, up);
     
     if (dir > 0.0) {
         return 1.0;
     } else if (dir < 0.0) {
         return -1.0;
     } else {
         return 0.0;
     }
 }

It returns 1.0 for CW, -1.0 for CCW and 0.0, if the angle between the two objects is 0.

Comment
Add comment · Show 5 · 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 BobBobson108 · Mar 08, 2013 at 08:55 PM 3
Share

here it is in C#

     float AngleDir (Vector3 fwd, Vector3 targetDir, Vector3 up) {
         Vector3 perp = Vector3.Cross(fwd, targetDir);
         float dir = Vector3.Dot(perp, up);
             
         if (dir > 0) {
             return 1;
         } else if (dir < 0) {
             return -1;
         } else {
             return 0;
         }
      }
avatar image MaeniAck · Mar 09, 2013 at 02:44 AM 0
Share

Sorry for not marking you answers a correct sooner.

As the question posted was an analogy for just a part of what I was doing I tried a lot of methods out, including yours and co$$anonymous$$g to the conclusion I was thinking about it in a wrong fashion. I ended up using a combination of vector calculations and trigonometry to achieve the effect I was looking for.

Nevertheless for the question I posted this was a fulfilling answer. And it did help me.

avatar image Daniel G · Jan 07, 2014 at 02:11 PM 0
Share

Hey @BobBobson108 Thanks for the C# version, ONE question, I read this whole question, and if im reading it right I could potentially check if my local objects y axis is pointing in the same direction as the Space.World y axis such as just vector3.up (Basically detecting if my gameobject is pointing in the same direction as the world or gobal axises) I only need to check if the two y axises (1.Being my GameObjects y axis) (2.Being a Vector3.Up) are pointing in the same direction. Let me know if im wrong thanks :D

avatar image Gkupce · Mar 04, 2017 at 11:17 PM 0
Share

Just a little tweak to make the function shorter (C#):

 float AngleDir(Vector3 fwd, Vector3 targetDir)
 {
     Vector3 perp = Vector3.Cross(fwd, targetDir);
     float dir = Vector3.Dot(perp, Vector3.up);
 
     return $$anonymous$$athf.Sign(dir);
 }
avatar image joaoborks · Jul 11, 2017 at 06:30 PM 1
Share

https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html

Now it does support Clockwise and Counter Clockwise returns

avatar image
0

Answer by Mehrdad995 · Jul 14, 2017 at 02:24 PM

I personally found this more simple and understandable

 public static float CalculateAngle180_v3(Vector3 fromDir, Vector3 toDir)
     {
         float angle = Quaternion.FromToRotation(fromDir, toDir).eulerAngles.y;
         if(angle > 180){return angle - 360f;}
         return angle;
     }

you can also return all angles as a vector3

Comment
Add comment · Show 1 · 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 Dorscherl · Apr 20, 2018 at 02:44 AM 0
Share

Couple questions (I know this is old): Will this calculate the angle for a specific axis? You said it all angles can be returned as a Vector3 via eulerAngles. Is the angle signed? @$$anonymous$$ehrdad995

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

10 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

Related Questions

Move infinitely in targeted direction 3 Answers

3rd person unit control 0 Answers

How can I respawn my RigidBodyFPSController facing new direction? 0 Answers

How to add slight angle/rotation variation to transform.forward? 5 Answers

Random /Organic/ Weapon Inaccuracy 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