Search Unity

Question How do I get the Inspector's Rotation?

Discussion in 'Scripting' started by alainjiehfeng, Jun 19, 2020.

  1. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Since the rotation is stored as a quaternion, it's very difficult at least for me to work with it (transform.rotation).

    Is it possible to get the value of the rotation angle in the Inspector? What I want is the camera's rotation angle as shown in the inspector, specifically the Y value. Thanks in advance. :)
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You can get the Euler angles of a rotation but that can cause you issues. What is it you are wanting to do with the Y angle?
     
  3. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Well basically I have an integer that goes from 1 to 4, and each value stores an area where I can aim in my game. So if the direction is 1, it will face in one direction, 2, the next direction and so on (like a NSEW compass).

    So currently the integer changes manually if I press shift, which increments the integer, but now I want to do something automatic where if I am facing a direction, the integer changes to match that direction, eliminating the player's need to cycle between the 4 directions to face and shoot.

    So to go about this, I figured the easiest way is to check the Main Camera's Y rotation, and have 4 if statements checking the value and setting the integer direction based on the set ranges. Catch my drift? :)

    I'll try and see what Euler does thanks. :)

    EDIT: I just checked eulerAngles and it isn't the same value as in the inspector.

    EDIT 2: Nvm shouldn't matter, I can use this. :)
     
    Last edited: Jun 19, 2020
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    It is the same rotation, but it might be a different representation. Ie. 180 == -180, 720 == 0, etc.


    I'd just store the forwards vector in the four directions, and check their angle to you player's current forwards direction. That's easier to read, and require less code to visualize.
     
    alainjiehfeng likes this.
  5. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Oh that sounds easy thanks, how do I do that exactly though? Is it possible that you could give me an if statement example? I'm not sure how to compare vectors, unless I have to choose the X or Z and compare but that seems a lot of work so I would like your confirmation first. :)
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    So the forwards vector of a transform is just transform.forwards. To have a vector facing eg. north, that'd be new Vector3(0, 0, 1)

    To compare angles, use Vector3.Angle(v1, v2). To find which of the four angles you're closest to, check the angles to all of them and find the smallest one.
     
    alainjiehfeng likes this.
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You can just use the dot product or vector angles to compare. I'm not sure what you want. Do you just want to check which is the closest direction your camera is facing in the N, S, E, and W directions?
     
    alainjiehfeng likes this.
  8. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Thank you! I just did what you said as far as I can and there was only one issue that the direction integer isn't the right one and it was somehow inversed, I just changed the values in order of 1, 2, 3, 4 in the if statements to 3, 4, 1, 2 and it is working as expected, not sure why though.

    Code (CSharp):
    1. void Start()
    2.     {
    3.         dirvector1 = new Vector3(-1.0f, -0.1f, 0.0f);
    4.         dirvector2 = new Vector3(0.1f, -0.1f, 1.0f);
    5.         dirvector3 = new Vector3(1.0f, -0.1f, 0.0f);
    6.         dirvector4 = new Vector3(-0.1f, -0.1f, -1.0f);
    7.     }
    8.  
    9. //in update:
    10.  
    11. if (mouse.rightButton.wasPressedThisFrame)
    12.             {
    13.                 float angle1 = Vector3.Angle(camera.transform.forward, dirvector1);
    14.                 float angle2 = Vector3.Angle(camera.transform.forward, dirvector2);
    15.                 float angle3 = Vector3.Angle(camera.transform.forward, dirvector3);
    16.                 float angle4 = Vector3.Angle(camera.transform.forward, dirvector4);
    17.  
    18.                 float smallest = Mathf.Max(angle1, angle2, angle3, angle4);
    19.  
    20.                 if (smallest == angle1)
    21.                     directions = 3;
    22.                 else if (smallest == angle2)
    23.                     directions = 4;
    24.                 else if (smallest == angle3)
    25.                     directions = 1;
    26.                 else
    27.                     directions = 2;
    28.             }
    While browsing I saw about dot product, seems confusing (probably only at first), I'll look into it later. As for what I want to do, I think it doesn't need explaining now as I've done it thanks to Baste. Thanks for your help as well. :)
     
  9. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    They will both do basically the same thing for you in this case. Dot uses slightly less code in its method so it slightly more performant although it isn't really going to make any odds here though.

    You could just use a for loop though and save yourself some if statements and could add to the vectors to check as well.

    Here's an example of using angle and dot;
    Code (CSharp):
    1.     public int CurrentDirectionAngle;
    2.     public int CurrentDirectionDot;
    3.  
    4.     [SerializeField]
    5.     private Vector3[] _directionVectors =
    6.     {
    7.         new Vector3(1.0f, -0.1f, 0.0f),
    8.         new Vector3(-0.1f, -0.1f, -1.0f),
    9.         new Vector3(-1.0f, -0.1f, 0.0f),
    10.         new Vector3(0.1f, -0.1f, 1.0f)
    11.     };
    12.  
    13.     private Transform _camera;
    14.  
    15.     private void Start()
    16.     {
    17.         _camera = Camera.main.transform;
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         CurrentDirectionAngle = GetDirectionAngle() + 1;
    23.         CurrentDirectionDot = GetDirectionDot() + 1;
    24.     }
    25.  
    26.     private int GetDirectionAngle()
    27.     {
    28.         var minAngle = Mathf.Infinity;
    29.         var ret = 0;
    30.  
    31.         for(var i = 0; i < _directionVectors.Length; i++)
    32.         {
    33.             var angle = Vector3.Angle(_camera.forward, _directionVectors[i]);
    34.  
    35.             if(angle <= minAngle)
    36.             {
    37.                 minAngle = angle;
    38.                 ret = i;
    39.             }
    40.         }
    41.  
    42.         return ret;
    43.     }
    44.  
    45.     private int GetDirectionDot()
    46.     {
    47.         var maxDot = -Mathf.Infinity;
    48.         var ret = 0;
    49.  
    50.         for (var i = 0; i < _directionVectors.Length; i++)
    51.         {
    52.             var dot = Vector3.Dot(_camera.forward, _directionVectors[i]);
    53.  
    54.             if (dot > maxDot)
    55.             {
    56.                 maxDot = dot;
    57.                 ret = i;
    58.             }
    59.         }
    60.  
    61.         return ret;
    62.     }
     
    alainjiehfeng likes this.
  10. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Thanks! I will try out this approach next time since I already got it working haha. The code makes sense to me, except the + 1 part in the Update statement, what's the logic there?
     
  11. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    An array starts at an index of 0 and you have your code set so the first index is 1 so we add one to the array index value which will give you the value you want your int to be at.
     
    alainjiehfeng likes this.