Facing angle of an object

Hi, I’d like to grab the facing angle of my unit object.

Let me illustrate:

19298-angles.png

Given the position of the blue character and the pink dot, I want to call some kind of function that will return 45.

Similarly, if the pink dot were placed below the blue character, I want the function to return 270. Et cetera for 180, 0, and 90 and all angles in between.

For context, I’m designing a mouse-movement system for my top-down game. I want to be able to access the ‘facing’ of the unit to determine what sprite/animation I should play. For example, if the unit is facing left, I want to play the ‘left’ animation.

Your numbers don’t make sense. 180 should be opposite 0, and 270 opposite 90. If you want the angle starting with 0 on the right and going counter clockwise you can use:

var v = pink.position - blue.position;
var angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;

3:00 will be 0.0.
12:00 will be 90.0;
9:00 will be 180.0;
6:00 will be 270.0;