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
2
Question by skoandi · Apr 24, 2013 at 07:22 PM · touchanglevectorvector2between

Get angle between 2 Vector2's

I'm making a skateboarding game right now and I'm having some problems.

I need to calculate the angle between the start touch position and the end touch position so if angle is ~0(swipe down), it will do a ollie (aka jump), else if angle is ~45(swipe down to the left), it will do a Frontside pop shuveit etc

Vector2.Angle gives me weird results, so please don't suggest that ;P

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

Answer by robertbu · Apr 24, 2013 at 07:35 PM

You can calculate the absolute angle using Atan2().

 v2 = v2End - v2Start;
 angle = Mathf.Atan2(v2.y, v2.x);
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 Yokimato · Apr 24, 2013 at 07:41 PM 0
Share

I think that makes the assumption that the origin is the relative point...might not be it in all cases. I think he's better off getting the true vectors and then get the angle.

avatar image skoandi · Apr 24, 2013 at 07:52 PM 0
Share

Thanks, it's not 0 to 360 but based on the number that I get (3 to -3) I can calculate when the player does different tricks :)

avatar image robertbu · Apr 24, 2013 at 08:00 PM 2
Share

An alternate solution would be to define a vector for all the different directions you want to distinguish. You could put them in an array to make calculation simple. Then you can do Vector2.Dot() between your stroke vector and all the vectors you have defined. The one with the hightest value will be the one closest to the user's stroke.

avatar image
8

Answer by QI · Mar 01, 2014 at 01:47 PM

HI, use the solution from robertbu ,use said you will get the value about -3 ~ 3.

     v2 = v2End - v2Start;
     angle = Mathf.Atan2(v2.y, v2.x)*Mathf.Rad2Deg;
     

This will make it to -180 ~ 180.

Yeah alright,I realise is a long time ago from the date you asked.Hope this will help some people !

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 skoandi · Mar 02, 2014 at 12:12 PM 0
Share

I bookmarked this question so it will definitely be useful one day :)

avatar image Andreywk · Aug 21, 2015 at 10:20 AM 0
Share

I made some changes:

 Vector2 v2 = (mousePosition - initPosition).normalized;
 float angle = $$anonymous$$athf.Atan2(v2.y, v2.x)*$$anonymous$$athf.Rad2Deg;
 if(angle < 0)
     angle = 360 + angle;
 angle = 360 - angle;
 Debug.Log("angle: " + angle);


Finally you will receive 360 degree around your init Position;

avatar image
4

Answer by Seyed_Morteza_Kamaly · Jan 22, 2018 at 08:47 AM

This code will debug your direction

directions

 using UnityEngine;
 using System.Collections;
 
 public class AngleDebugger : MonoBehaviour
 {
     public Transform target;
     private float Angle;
 
 
     void Update()
     {  
         Angle = GetAngle(transform.position,target.position);
         DebugAngle();
     }
 
     public float GetAngle(Vector2 A,Vector2 B)
     {
         //difference
         var Delta = B - A;
         //use atan2 to get the angle; Atan2 returns radians
         var angleRadians = Mathf.Atan2(Delta.y, Delta.x);
 
         //convert to degrees
         var angleDegrees = angleRadians * Mathf.Rad2Deg;
 
         //angleDegrees will be in the range (-180,180].
         //I like normalizing to [0,360) myself, but this is optional..
         if (angleDegrees < 0)
             angleDegrees += 360;
 
         return angleDegrees;
     }
 
 
     void DebugAngle()
     {
         if (Between(60, 120))
             {
             print("N");
         }
         if (Between(240, 300))
         {
             print("S");
         }
         if (Between(330, 360) || Between(0, 30))
         {
             print("E");
         }
         if (Between(150, 210))
         {
             print("W");
         }
 
 
         if (Between(120, 150))
         {
             print("NW");
         }
         if (Between(30, 60))
         {
             print("NE");
         }
 
         if (Between(210, 240))
         {
             print("SW");
         }
         if (Between(300, 330))
         {
             print("SE");
         }
 
 
     }
 
     bool Between(float A,float B)
     {
         if(Angle < B && Angle > A)
         {
             return true;
         }
         return false;
     }
 
 }



iso-directions-600px.png (16.1 kB)
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 Yokimato · Apr 24, 2013 at 07:38 PM

Vector2.Angle is giving your "weird" results because you're using it wrong -- sorry, but it's true.

That function assumes you have vectors, not positions. Which should be very understandable because you asking the angle between two points is, well, just not possible ;)

So I guess the point is, what angle do your really want (i.e what's the point that's actually relative to these start and end points [is there a center?]) If you know that, then we CAN get the vector for each, and then we CAN used Vector2.Angle!

Oh math <3

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 robertbu · Apr 24, 2013 at 07:59 PM 0
Share

I agree is probably getting "weird results" because he is misusing the function, but Vector2.Angle() has a serious issue. It returns an unsigned value and it is the shortest path between the two vectors. It makes it difficult to distinguish between different strokes.

avatar image skoandi · Apr 24, 2013 at 08:00 PM 0
Share

hopefully this image will explain what I'm trying to do: alt text

namnlös.png (15.9 kB)
avatar image robertbu · Apr 24, 2013 at 08:08 PM 0
Share

Oh I see. You Could use the angle between the "perfect" stroke and the user's stroke and make it work.

avatar image skoandi · Apr 24, 2013 at 08:11 PM 0
Share

but how do I get the angle?

avatar image flyingbanana · Jun 24, 2015 at 05:32 AM 0
Share

I keep forgetting about this even though I am fully aware of it as soon as I realise it. Oh math <3

avatar image
0

Answer by Hani_Shojaei · Aug 24, 2020 at 10:17 AM

you can use this functions:

     public static float AngleBetween2Vector2right(Vector2 pos, Vector2 target)
     {
         if (target.y < pos.y)
             return Vector2.Angle(Vector2.right, target - pos) * -1.0f;
         else
             return Vector2.Angle(Vector2.right, target - pos);
     }
     public static float AngleBetween2Vector2Up(Vector2 pos, Vector2 target)
     {
         if (target.y < pos.y)
             return Vector2.Angle(Vector2.right, target - pos) * -1.0f - 90;
         else
             return Vector2.Angle(Vector2.right, target - pos) - 90;
     }
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

17 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

Related Questions

Rotate an object around another object at an angle from the X axis? 1 Answer

How to find direction Vector for a collision surface? 1 Answer

Get angle of Vector2 1 Answer

2D angle of two Vec3 0 Answers

the projectile reflects 1 time only when it hits something, pls help! 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