banner



how to find angle between two vectors

Search Unity

Unity ID

A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate in the Unity community.

  • csharp
  1. I have Vector2 position1 and Vector2 position2. How can I get the angle between them?

    I managed to find method Vector3.SignedAngle(targetDir, forward, Vector3.up) but it returns floats while I need degrees 0...360. It's not radians, what numbers are they? How can I convert them to degrees?

  2. According to the documentation https://docs.unity3d.com/ScriptReference/Vector2.Angle.html returns the angle in degrees as does https://docs.unity3d.com/ScriptReference/Vector2.SignedAngle.html
                            float                      
    is simply a data type (floating point number). It doesn't mean anything about the units of the value.
  3. Vector3.SignedAngle returns degrees, although it's going to return them in the interval of -180 to 180, not 0 to 360. There is probably something wrong with your arguments or how you calculated them.

  4. According to documentation:
    1. The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than - 180 degrees
    But a bit down, in a sampleThis is what I get usually, numbers 1.0f, 5.0f, 10.0f. Why I can't get degrees?
  5. Those are degrees... 1 degree, 5 degrees, 10 degrees.

    What are your inputs? What are you expecting the result to be instead of what you're getting? What does your code look like?

  6. The C# language doesn't have any kind of "degrees" data type, if that's what you're confused about.
    Pretty sure no programming language does, since they're just numbers at the end of the day.
  7. Actually what I'm doing is, I need to draw a line from point 1, to point 2. I can get the distance, but I also need the angle and he I got stuck.

    Let's say I have two points new Vector3 (0.0f, 0.0f, 0.0f) and new Vector3 (100.0f, 100.0f, 0.0f). How to get the angle between them?

  8. I'm not sure what "the angle between two points" means. That doesn't really make sense. Could you maybe draw a picture of what you're talking about? Why would you need an angle to draw a straight line?

    Generally you need 3 points to calculate an "angle", treating two of them as "directions" away from a third point. The third point is assumed to be the origin (0, 0) for the Vector2.Angle and Vector2.SignedAngle methods.

    Last edited: Jul 11, 2020
  9. Here is the better sample. I have two lines

    Line 1 Vector2(0,0) -> Vector2(100, 0) (starting, ending)
    Line 1 Vector2(0,0) -> Vector2(100, 100) (starting, ending)

    How to get the angle between these two lines?

  10. With Vector2.Angle.
    1. Vector2 a = new Vector2( 100, 0 ) ;
    2. Vector2 b = new Vector2( 100, 100 ) ;
    3. float angle = Vector2. Angle (a, b) ;

  11. Now I got the idea how it's calculated, thanks a lot!!
  12. Keep in mind that (0,0) or (0,0,0) is not a vector. You will get zero Angle if you'll use it as one of the from/to vectors.
  13. Technically it is a vector, but you can't get an angle relative to it because it is not a direction.
  14. I got lost, I thought I was near the answer, but failed. Here is the picture of problem. I have two dots (x,y) and (x1, y1) and I need I draw red line between them. Actually red line is a thick image and I need just specify Z angle of it and it will be nicely rotated. Any idea how to do it?

    I need any of angle marked with question mark

  15.                           float angle = Mathf.Rad2Deg * (Mathf.Atan2(pointB.y - pointA.y, pointB.x - pointA.x));                        
    Last edited: Jul 11, 2020
  16. Thanks a lot. Now all points works as charm. I just had to add following correction "-(90 - angle)"

how to find angle between two vectors

Source: https://forum.unity.com/threads/get-angle-in-degress-between-two-vectors.929190/

Posted by: dinhuponce.blogspot.com

0 Response to "how to find angle between two vectors"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel