Get angle in degress between two vectors

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?

According to the documentation Unity - Scripting API: Vector2.Angle returns the angle in degrees as does Unity - Scripting API: Vector2.SignedAngle

float is simply a data type (floating point number). It doesn’t mean anything about the units of the value.

1 Like

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.

According to documentation:

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 sample

        if (angle < -5.0F)
            print("turn left");
        else if (angle > 5.0F)
            print("turn right");
        else
            print("forward");

This is what I get usually, numbers 1.0f, 5.0f, 10.0f. Why I can’t get degrees?

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?

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.

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?

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.

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?

With Vector2.Angle.

Vector2 a = new Vector2(100, 0);
Vector2 b = new Vector2(100, 100);

float angle = Vector2.Angle(a, b);
1 Like

Now I got the idea how it’s calculated, thanks a lot!!

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.

Technically it is a vector, but you can’t get an angle relative to it because it is not a direction.

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


float angle = Mathf.Rad2Deg * (Mathf.Atan2(pointB.y - pointA.y, pointB.x - pointA.x));

8 Likes

Thanks a lot. Now all points works as charm. I just had to add following correction “-(90 - angle)”