Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
7
Question by jc_lvngstn 1 · Apr 23, 2010 at 10:21 AM · rotateangletowards

How to get the positive or negative angle between objects?

I need to decide if I want something to swivel left or right towards my target. Currently, angle only seems to return positive values. I need to find out, if two objects are level, if I need to add angles to the y rotation of my object, or subtract angles in order for it to eventually face towards my target.

Is there a straightforward function in Unity I've overlooked?

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

3 Replies

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

Answer by duck · Apr 23, 2010 at 11:28 AM

You can use Mathf.Atan2 to compute the angle between 2D vectors. This only works for 2d rotations though, however often that's all you need even in a 3d game. Eg:

var localTarget = transform.InverseTransformPoint(target.position);
var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z);

However, the "angle" will be in radians, not degrees, so you most probably want to convert it to degrees before using it, like this:

var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

This should give you the target angle in the range -180 to 180 degrees.

Comment
Add comment · Show 6 · 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 mrde · Mar 13, 2011 at 11:46 AM 0
Share

It helps me too. Thanks.

avatar image BLF-Games · Jun 13, 2011 at 06:44 PM 0
Share

Duck, you're a genius! I spend several hours trying to acomplish angle calculations to rotate enemy towrads waypoint! in 2 damn fantastic lines! 1000 times THAN$$anonymous$$S!

avatar image SergeantBiscuits · Jul 30, 2012 at 07:38 AM 1
Share

After DAYS of searching, I found the solution! Thank you, Ben $$anonymous$$t! Finally somebody answers this question without telling the person that their question "doesn't make sense."

avatar image demilp · Nov 29, 2013 at 08:10 PM 0
Share

InverseTransformPoint would be something like this in OpenT$$anonymous$$?

     public Vector3 InverseTransformPoint(Vector3 position)
     {
         $$anonymous$$atrix4 mat = $$anonymous$$atrix4.Transpose(Generate$$anonymous$$atrix());
         $$anonymous$$atrix4 matT = $$anonymous$$atrix4.Translation(position) * mat;
         return new Vector3(matT.$$anonymous$$14, matT.$$anonymous$$24, matT.$$anonymous$$34);

     }
avatar image Ruffles182 · Feb 21, 2018 at 10:01 AM 0
Share

Omg!! i love you! xD

Show more comments
avatar image
5

Answer by spinaljack · Apr 23, 2010 at 10:33 AM

You can test to see if the angle returned it more than 180 and then reverse the rotation your self (-360).

Something like:

function getAngle(){ // angle code if (Angle > 180) Angle -= 360; return Angle; }

// do your rotation

Comment
Add comment · Show 3 · 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 Horsman · Apr 23, 2010 at 02:34 PM 1
Share

Angle will give you a value of at most 180 degrees.

avatar image Antitheory · Mar 22, 2011 at 03:08 AM 0
Share

This the common-sense solution. Some people try too hard.

avatar image zacharyaghaizu · Aug 05, 2021 at 08:15 AM 0
Share

just what I was looking for!

avatar image
3

Answer by _MGB_ · May 06, 2010 at 07:39 AM

A nice trick for 3d: the cross product of two vectors is a vector quantity with a magnitude equal to the product of the magnitudes of the two vectors times the sine of the angle between them.

So for two normalised vectors:

angle_diff = asin(cross(v1, v2).len())

The resulting vector can be used as an axis for rotation between the two input vectors.

Comment
Add comment · 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 jc_lvngstn 1 · May 06, 2010 at 03:44 PM 0
Share

Thanks for this info, $$anonymous$$GB

avatar image 1337GameDev · Jan 01, 2012 at 04:04 AM 1
Share

whhhaatt? what does cross product do? why is it useful? How can this be applied to posative / negative angles?

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

8 People are following this question.

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

Related Questions

Rotating without gimbal lock 1 Answer

using mouseorbit script and Rotate Around with specific angle 1 Answer

Rotate from angle to angle 1 Answer

Rotate 90 over time on mouseDown 2 Answers

Rotate parent so child is facing a specific rotation 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