Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
8
Question by MonkeyAssassin8 · Nov 01, 2011 at 10:02 AM · anglenegative

Is there way to find a negative angle?

I want to be able to find the negative angle similar to Vector3.Angle. I want this for a hit detection gui that rotates in the direction of the enemy that is shooting the player. Vector3.Angle works, but it will only work one way (it will rotate to the right, but not left)

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

5 Replies

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

Answer by aldonaletto · Nov 01, 2011 at 11:55 AM

I had this problem once too: Vector3.Angle returns an absolute value. You can use a cross product to know the angle polarity - the cross product of vectors A and B results in a vector C perpendicular to A and B, but pointing to one side if A is "before" B, and to the other if A is "after" B. If vectors A and B are roughly in the horizontal plane, for instance, you can use the polarity of C.y as the angle signal:

    var angle:float = Vector3.Angle(vectorA, vectorB);
    var cross:Vector3 = Vector3.Cross(vectorA, vectorB);
    if (cross.y < 0) angle = -angle;
Maybe you need to invert the angle or swap vectorA and vectorB to get the polarity you expect.
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 CHPedersen · Nov 01, 2011 at 12:15 PM 0
Share

Damn it, ninja'ed by aldo. ;) +1 for being quicker in conveying the same idea. :P

avatar image Corey Knight · Oct 17, 2012 at 09:16 AM 0
Share

Great answer, very concise! Works as a drop-in replacement for Vector3.Angle, with the expected polarity.

avatar image Denis Kniazhev · May 01, 2015 at 07:22 PM 0
Share

Shouldn't it be if (cross.z < 0) ?

avatar image Eno-Khaon · May 01, 2015 at 08:18 PM 1
Share

Nope. This is working on the assumption you're comparing forward and right (Z and X respectively). The cross product of those vectors is either up or down-facing, so it's based on whether the Y value for the cross product is positive or negative (as well as the order of variables in the cross product, but that's already accounted for)

avatar image nik_2522 · Apr 19, 2016 at 08:12 AM 0
Share

Sir, I am facing a similar situation which I have mentioned in my problem here. It hasnt got any answers as of yet.

Restrict Rotation

$$anonymous$$y problem isn't being solved by cross product. I need to detect a change in direction WHEN a body/gameObject in rotation changes its direction of rotation. Please Help !!

Show more comments
avatar image
15

Answer by joaoborks · Jul 13, 2017 at 04:54 PM

Now done natively: https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html

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 jeromeWork · Jul 02, 2019 at 10:05 AM 1
Share

Thanks. This should now be marked as 'best' answer

avatar image
7

Answer by CHPedersen · Nov 01, 2011 at 12:14 PM

Sure. :)

What you are interested in is actually the orientation of the vectors with respect to one another; that is, you want to know if, given vectors 1 and 2, does vector 2 lie to the left or the right of vector 1? If it's to the left, you consider the angle between them to be positive, and contrarily, if it is to the right, you consider the angle to be negative.

Crossing the vectors is one way to determine this, but you have to project the vectors onto some plane of reference first. Let's say you have the two vectors v1 = (1,0,0) and v2 = (0,1,0). It's obvious to everyone that the angle between them is positive 90 degrees because v2 is "to the left" of v1, right? But that's only when viewing their projection onto the z-plane, i.e. dropping their z-coordinate and considering only (1,0) and (0,1). If viewed from the y-plane, the angle stops making sense at all because one of the vectors becomes the zero-vector.

So, before you do any kind of testing to determine their orientation with respect to one another, you have to decide on some plane of reference within which their angles are to count. Let's say I decide to perform this test with their projections onto the z-plane. Then, calculate the cross product. If the cross product's z value is positive, it means the angle between them is positive as well. If the cross product is negative, it means the angle is negative. Consider this piece of code:

 Vector3 v1 = new Vector3(1, 0, 0);
 Vector3 v2 = new Vector3(0, 1, 0);
 
 private void Start()
 {
     int sign = Vector3.Cross(v1, v2).z < 0 ? -1 : 1;
     Debug.Log(sign * Vector3.Angle(v1, v2));
 }

This prints 90, and -90 if you swap the two vectors. Make sense?

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 aldonaletto · Nov 01, 2011 at 02:52 PM 0
Share

Good explanation! I ninja'ed you because this time my answer was more concise, what is really a rare thing! Conciseness isn't a quality of ours... (what's a good thing for the OPs)

avatar image
-1

Answer by dr4 · Jul 05, 2016 at 06:09 AM

it is a really old question but Google sent me here as first option so it may happen with someone else in the future, in my case I was trying rotate something to the left Vector3(0, -90, 0) wasn't working so I looked everywhere trying to find a solution and nothing helped me, it was simple as put it as a float: Vector3(0, -90f, 0) it turn 90 degrees to left now.

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

Answer by DRP.du · Nov 01, 2011 at 12:23 PM

transform.eulerAngles.x (y or z) ?

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 aldonaletto · Nov 01, 2011 at 02:42 PM 1
Share

This works for knowing the rotation of some object, but it seems the OP wants to know the angle between two vectors with Vector3.Angle(v1, v2) - and this function only returns an absolute value.

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

13 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

Related Questions

Bullet not coming out of gun properly after rotation 0 Answers

How to control the angle of a joint using a variable 2 Answers

2D TOP DOWN - W A S D as direction 2 Answers

how to convert the angle to roatation? 1 Answer

rotate to specific coordinate c# 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