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.


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?


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

  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Aug 18, 2020 at 17:17
  • \$\begingroup\$ 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... \$\endgroup\$ Aug 18, 2020 at 18:30
  • \$\begingroup\$ I've added more details now, see if it's help you to understand my problem. \$\endgroup\$ Aug 18, 2020 at 19:24
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 18, 2020 at 22:07

1 Answer 1


Unity has many built-in functions to assist you with this sort of thing. Whenever you run into a problem like this, it's a good idea to stop and think "is this an issue that's likely to affect most developers? Is it reasonable to assume there are built-in functions to make this easier?"

Some good ones, all which automatically wrap around 360 degrees:

  • \$\begingroup\$ Thanks, I was trying to build this so hard that I forgot to search for built in functions. I believe that just one of these can help me though, this first one. Cause I'm not aiming to move the player by code, instead, I need to move it using root motion. I'm trying to build a system that sends the rotation data to the animation system, so the right rotation animation can be played. I'll complete the question now. \$\endgroup\$ Aug 18, 2020 at 18:38
  • \$\begingroup\$ Thank you again, I did some simple tests and Mathf.DeltaAngle seems to be the solution I need. Sorry to bother with something basic as that.. \$\endgroup\$ Aug 18, 2020 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .