# Efficient way to build a 360 rotation system with clockwise and counterclockwise direction

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...

• It would be easier if you showed your code - even if it's flawed or incomplete - just so we can understand what you conceive of as "a rotation system," since that's not a standard game development term. What are its inputs and outputs? Are you working with absolute headings or relative deltas? Are you rotating incrementally over time or instantaneously in jumps? Are you in 2D or 3D? etc. The better you can define your needs, the better we can help you meet them. Aug 18, 2020 at 17:17
• Sorry, you're right. I tried to avoid a question with too many lines, plus my previous attempts are too wrong I guess. I'll edit the question with more details... Aug 18, 2020 at 18:30
• I've added more details now, see if it's help you to understand my problem. Aug 18, 2020 at 19:24
• Thank you for the willingness. Of course that the solution was ridiculous, aparently what I need is just Mathf.DeltaAngle() with the angles that I already have. This method already does what I was trying to do by my own. Aug 18, 2020 at 22:07