I feel like this question may have been asked before, but I could not find anything about it, so I figured I’d ask.
Now I’ve got a character which I want to look at my mouse in only four directions, so a bit like:
(Sorry for my arty skills)
Now I’ve got a piece of code:
private void UpdatePlayerAnimation(Vector2 Motion)
{
float MotionAngle = (float)Mathf.Atan2(Motion.y, Motion.x);
if(Motion == Vector2.zero)
{
CurrentLookSide = LookSide.Idle;
}
else if(MotionAngle >= -PiOver4 && MotionAngle <= PiOver4)
{
CurrentLookSide = LookSide.Right;
Motion = new Vector2(1, 0);
}
else if(MotionAngle >= PiOver4 && MotionAngle <= 3 * PiOver4)
{
CurrentLookSide = LookSide.Up;
Motion = new Vector2(0, 1);
}
else if(MotionAngle <= PiOver4 && MotionAngle > -3 * PiOver4)
{
CurrentLookSide = LookSide.Down;
Motion = new Vector2(0, -1);
}
else
{
CurrentLookSide = LookSide.Left;
Motion = new Vector2(-1, 0);
}
}
In this the ‘Motion’ is just the velocity of my Rigidbody2D.
Now I tried moddifying the MotionAngle to match the mouse position (both the Input.mousePosition and doing a raycast), but this does not seem to work. Now my question is: am I doing something wrong (working with angles)?. My lucky guess is that the calculations are wrong for the mouse, but I can’t seem to fix it.
Anyone with some input? Thanks in advance!
P.S. I’m working in a 2D environment.