- Home /
Facing angle of an object
Hi, I'd like to grab the facing angle of my unit object.
Let me illustrate:
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.
Answer by robertbu · Dec 13, 2013 at 08:37 PM
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;
Perfect, thanks!
You're right, I'm not sure what I was thinking when I made that image. But I'm glad you were still able to understand what I was trying to ask.