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
3
Question by komodor · Apr 02, 2014 at 10:38 AM · 2dvector2trigonometry

Vector2.angle - how do i get if it's CW or CCW ?

is there any way to get angle between 2 vectors where i get N for clockwise -N for counterclockwise? or any other way how to get this info?

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
8
Best Answer

Answer by ArkaneX · Apr 02, 2014 at 01:59 PM

Basing on answer to this StackOverflow question, you can also use:

 private static float GetAngle(Vector2 v1, Vector2 v2)
 {
     var sign = Mathf.Sign(v1.x * v2.y - v1.y * v2.x);
     return Vector2.Angle(v1, v2) * sign;
 }


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 Owen-Reynolds · Apr 02, 2014 at 02:20 PM 0
Share

This is the standard cross-product trick. The direction of the cross product tells you + or - angle. Since both are "flat" 2D vectors, you know the cross product is either pointing straight up or straight down -- Z is +1 or -1 -- and x and y are 0. That line computes the Z component of cross product.

Think it should be "times negative sign." Try it with up and east: (0,1) and (1,0). Result should be +90. But the math gives 0-1. Then try flipped with East to Up -- (1,0) and (0,1). Result should be -90. $$anonymous$$ath gives 1-0 this time. It's backwards since real math thinks a positive angle is CCW.

avatar image ArkaneX · Apr 03, 2014 at 08:10 AM 1
Share

It's backwards since real math thinks a positive angle is CCW.

Assu$$anonymous$$g we use left handed coordinate system.

avatar image
1

Answer by Xare · Apr 02, 2014 at 12:24 PM

it's always the smallest angle between the two points so it never returns something bigger than 180degrees.

What you can do is to get a static third point and calculate the angles between the 1st and 3rd point and then 2nd and 3rd point and then compare the values to see the angle between the 1st and 2nd points.

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
1

Answer by VesuvianPrime · Apr 02, 2014 at 12:08 PM

There's probably a better way, but off the top of my head:

  1. Rotate both Vector2s such that the start vector points up

  2. Check if the x component of the second vector is greater or smaller than the x component of the first vector.

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
1

Answer by whydoidoit · Apr 02, 2014 at 12:15 PM

This will do it for Vector2s if you leave n as the default

 public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n = default(Vector3))
 {
     n = n == default(Vector3) ? Vector3.forward : n;
     return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2))*Mathf.Rad2Deg;
 }
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
1

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

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

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

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

26 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

Related Questions

Using Lerp with 2d only moving partially 1 Answer

Replacement for LookAt in 2D 1 Answer

Move prefab in the direction its facing (2D) 2 Answers

Move object by touch input 5 Answers

How to get new position of object based on start point, distance and angle? 2 Answers


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