Why is Vector2.Angle() returning 90 regardless of input for me?
Hey all,
Fairly new to Unity and just trying to calculate the angle between a point of origin and a target in 2D space. The code I'm using is:
public class BrokenThing : MonoBehaviour
{
public GameObject player;
void Update () {
Vector2 offset = player.transform.position;
Vector2 origin = new Vector2(0, 0) + this.offset;
Vector2 target = new Vector2(0, 0);
float angle = Vector2.Angle(origin, target);
Debug.Log(angle);
}
}
I would expect that when player moves around the angle would update but as it stands the angle always returns 90. I can also see that the x and y values for origin are changing as the player moves around the environment. Could someone let me know where I'm going wrong?
Answer by Backside2357 · Jan 21, 2018 at 08:31 PM
The problem is that Vector2(0, 0)
is not a vector, but the origin point. So any other vector that is not also zero, is perpendicular to this point, so 90 degrees makes sense. Try something like Vector2(1, 0)
instead.
Ah I see my mistake. I assumed that points and vectors were synonymous but I think now that vectors refer to from origin to the x y coordinates.
That would explain always getting 90. Thanks for your help :)
Your answer

Follow this Question
Related Questions
Get Direction of a vector 0 Answers
Why doesn't my script turn the way i want it too 0 Answers
how to calculate the angle between two vectors? 6 Answers
[SOLVED]Unexpected results 0 Answers
Vector in Image button script error 1 Answer