Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How does Unity work with (or treat) Radians

Discussion in 'Editor & General Support' started by godfreyidk, Aug 13, 2018.

  1. godfreyidk


    Feb 4, 2018
    Hello folks,

    So i've been going over the ThirdPersonCharacter.cs script from the Unity Standard Assets, and noticed that radians were not being converted to degrees. Then i saw that despite this conversion not taking place, the radians were being conditionally multiplied by 180 and 360, which makes the radian float "large" enough to do a degrees-equivalent rotation of say 50, 90, 180 etc. Thus a conversion might not be necessary. But then i noticed how when the input direction was "left" (-1, 0, 0), the character would rotate left starting from its forward. Instead of going "clockwise all the way around from 0 to the radians/degrees mapped at (-1, 0, 0) on the object's unit circle - which would be 270 degrees or 3/2pie radians. This expectation is from my naive understanding of the unity unit circle, whose degrees/radians are mapped starting from 0 at the object's forward, and increasing clockwise.
    So, i decided to serialize the radians (float) returned by Mathf.Atan2(x,y) - corrected for clockwise angle progression. And i saw that the radians (float) returned by Mathf.Atan2(x,y) was actually a negative value when the input direction was "left". Which to me, meant that Unity was dividing the unit circle of mapped radians/degrees, into a negative-left hemisphere, and a positive-right hemisphere. Such that if Mathf.Atan2(x,y) returned a negative radian float, that radian would start/apply from the object forward (0 radians on the unit circle) and progress anticlockwise on the negative-left hemisphere. When Atan2 returned a positive radian float, the radian/float would be applied clockwise from the object forward, on the positive-right hemisphere.

    Just for clarity sake, here's an illustration of "unity unit circle" that i keep mentioning:
    radians4.PNG radians4 (2).PNG radians5.PNG

    Thanks for reading this far. I apologize if it was unclear.
    I've been revising my high school trigonometry and studying this ThirdPersonCharacter.cs script over the weekend. I would really appreciate some confirmation on whether this assumption of mine (negative-left and positive-right hemispheres of the unity unit circle) is correct or not.

    Here's the relevant excerpt from the ThirdPersonCharacter.cs script that you can test for yourself. Be sure to drop in the camera transform in the editor for camTransform.

    Code (CSharp):
    1. public class howUnityTreatsRadians : MonoBehaviour {
    2.     [SerializeField] private Vector3 inputDir;
    3.     public Transform camTransform;
    4.     [SerializeField] private float turnAmount;
    6.    void Start () {
    8.     }
    10.     void Update () {
    11.         inputDir = camTransform.right * Input.GetAxis("Horizontal") +
    12.                    camTransform.forward * Input.GetAxis("Vertical");
    14.         if (inputDir.magnitude > 1f) inputDir.Normalize();
    15.         inputDir = transform.InverseTransformDirection(inputDir);
    17.         inputDir = Vector3.ProjectOnPlane(inputDir, Vector3.up);
    19.         turnAmount = Mathf.Atan2(inputDir.x, inputDir.z);
    20.     }
    21. }
  2. TenKHoursDev


    Nov 9, 2014
    If you want to convert from radians to degrees you just multiply by Mathf.Deg2Rad and inversely: Mathf.Rad2Deg.

    Next I think you have some confusion going on here. The axis alignment of the unit circle is because you've mapped Vector3.forward to vertical and Vector3.right to the horizontal. This changes where zero on the circle is.

    These unity unit circle and the traditional unit circle are the exact same things. You've picked up on the fact that in the ThirdPersonCharacterController zero is forward (Vector3.forward), and that is because in Unity the Z axis is the forward direction. If you wanted to have a proper Unit circle for your character controller, you would map transform.forward to the vertical axis, and the horizontal axis to transform.right.

    Its a simple concept, but easy to get confused. I recommend that you keep studying, but at least you asked a question somewhere to clear up any confusion.

    --ComputerScience major/Math minor at University
    godfreyidk likes this.