Arguments for Unity's Atan2 and unity vector subtraction the wrong way round?

Hey there,

I’m trying to work out the angle of one object in relation to another.

if(object A is at (5,0) and object B is at (10,0) then (assuming that 0 degrees is (0,1))
then object B is at 90 degrees to object A.

They way I was taught to do this was to work out the vector (C) between them A-B=C and then do atan2(c.y,c.x).

The way I was taught vector subtraction is as follows:

A-B =

B.x-A.x = C.x
B.y-A.y = C.Y

but it seems that in unity that:

A-B =

A.x-B.x = C.x
A.y-B.y = C.Y

Atan2 lists its arguments as Atan2(y,x), however if I use it as specified, then I seem to get incorrect results. if I do Atan2(x,y) and I assume that vector subtraction works differently to how I expect (A.x-B.x = C.x; A.y-B.y = C.Y), I get the correct results.

Hmm, maybe this will be clearer :stuck_out_tongue:

    public Transform enemy;
	
	Vector2 me;
	Vector2 enemyPos;
	Vector2 bearingV;
	float bearing;

	// Use this for initialization
	void Start () {
		me = new Vector2(transform.position.x,
                transform.position.z); // (10, 0)
		enemyPos = new Vector2(enemy.transform.position.x,
                enemy.transform.position.z); // (5,0)
		bearingV = enemyPos - me;
		 
		float bearing = Tools.rad2Deg(Mathf.Atan2(bearingV.x,bearingV.y));    		
		Debug.Log(bearing); //comes out at 90
		
	}

So my questions are:

  1. which way round does vector subtraction work in unity
  2. are the arguments for atan2 in unity (y,x) or (x,y)

Thanks in advance for any clarification you can provide.

Atan2 is definitely y,x as specified in the reference. Vector subtraction works the same as I’ve always seen it work:

C = A - B 

I the same as:

C.x = A.x - B.x
C.y = A.y - B.y
C.z = A.z - B.z

And if you do this:

Vector2 me = new Vector2(10,0);
Vector2 enemyPos = new Vector2(5,0);

void Start () {
   Vector2 bearingV = enemyPos - me;
   float bearing = Mathf.Atan2(bearingV.y,bearingV.x) * Mathf.Rad2Deg;           
   Debug.Log(bearing);
}

You get 180, which is exactly what you should get.