I've been struggle with rotation for sometime now, maybe it isn't that hard for someone who dominates the mathematical side of things regarding rotation and angles but, I need to build a rotation system that rotates the player to either clockwise or counterclockwise direction according to the nearest direction to the target angle.
I.E.
If my player is rotated to 45° and the direction is changed to 135°, he must rotate 90º clockwise (from 45° to 135°). If my player is rotated to the same 45° and the direction is changed to 315°, he must rotate 90° counterclockwise (from 45° to 315º).
This didn't seemed a big deal when I started, but it's been hard for me to build something that cover all the rotation possibilities, which tells me that I'm doing something wrong here. I mean, there's must be a couple of formulas that fit this problem and cover all the possible rotations or something.
Right now, my rotation system is configured to a minimal of 5 degrees of rotation, wich means that the possible degrees goes from 0 to 355(it never get to 360, going back to 0 instead).
I don't have a code to show cause everytime I try to build this, I reach to a point where there are so many possibilities that aren't covered by my tests that I end up deleting everything to try again, but I'm tired of this seemingly endless loop (for me anyway lol).
Does anyone knows a way to build this?
-EDIT-
My project is 3D and the camera perspective is similar to sports games like NBA 2K or soccer games in general. The input is a Vector2 obtained with the new input system. I need to move my player around with root motion, which means that my goal with this system is to calculate the rotation data to be passed as an animation parameter (using a event), so the correct animation is played and the player finally rotate according to the animation. The main part of the code (calculation part) was built by a friend of mine, who has more knowledge than me. This is my current code:
if (movementVector != Vector2.zero)
{
targetRotationAngle = Mathf.Atan2(movementVector.x, movementVector.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y; //FUNCTIONAL CODE BUILT BY A FRIEND
targetRotationAngle = Mathf.CeilToInt(targetRotationAngle / 5) * 5; //FUNCTIONAL CODE BUILT BY A FRIEND
if (targetRotationAngle < 0)
targetRotationAngle += 360;
if (currentRotationAngle == targetRotationAngle)
return;
else
{
if (currentRotationAngle < targetRotationAngle)
{
Debug.Log("Clockwise rotation in " + (targetRotationAngle - currentRotationAngle) + " degrees");
}
if (currentRotationAngle > targetRotationAngle)
{
Debug.Log("Counterclockwise rotation in " + ((360-currentRotationAngle)+targetRotationAngle) + " degrees");
}
}
currentRotationAngle = targetRotationAngle;
}
This is a summarized version of my previous attempts (the ELSE part), I know my formula is wrong, cause it doesn't cover all the rotation scenarios, the more I tried to build, more of this IF's were nesting. That's why I've decided to ask...