# Position in relation to another object

My game uses 2D enemies in 3D space. The enemies graphics are obviously flat and are always facing the player. However, I want a different graphic to be displayed for each enemy based on a variable facingDirection which has an enum value corresponding to 8 compass points (N, NE, E, SE, S, etc.) as well as the player’s position in relation to the enemy.

To be more clear: If the enemy is facing north, and the player is standing to the north of the enemy, I want the enemy’s graphic to display its front. If the enemy is facing north and the player is standing to the south of the enemy, I want the player to see its back.

I have an algorithm to calculate this, but it’s somewhat complex and I don’t want to waste time implementing it if there is a built-in function within Unity.

My question is:
Is there such a function in Unity (To find one gameobject’s position in relation to another)?

Any help is appreciated.

To find one gameobject’s position in relation to another

Perhaps I misunderstand, but this sounds like a simple Vector3 subtraction operation to me.

``````Vector3 player_relative_to_enemy=enemy.transform.position - player.transform.position;
``````

You can “Normalize” the result to get a vector of magntiude 1, a “direction”.

``````player_relative_to_enemy.Normalize();
``````

if you want an angle as the result (the Atan2 operation does NOT need normalization)…

``````float angle=Mathf.Atan2(player_relative_to_enemy.x,player_relative_to_enemy.y);
``````