Search Unity

Quaternion.AngleAxis

Discussion in 'Scripting' started by GerPronouncedGrr, Sep 20, 2016.

  1. GerPronouncedGrr

    GerPronouncedGrr

    Joined:
    Mar 30, 2013
    Posts:
    25
    According to the documentation the function referenced in the title should rotate a certain number of degrees around the specified axis. In the script below, however, the function is setting the rotation of the object to the specified number of degrees. Meaning, it rotates once and never again. Can anyone advise me how to modify this script so that the angle is updated each time the button is pressed?

    Just to make things clear, I'm using the asset "ReWired", which is an advanced input manager. So, on line 31, what you're seeing is a function that rotates the map clockwise when the Right Trigger on a gamepad is pressed, and counter-clockwise when the Left Trigger is pressed.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using Rewired;
    4.  
    5. public class BattleMapControls : MonoBehaviour
    6. {
    7.     public int PlayerID = 0;
    8.     public float RotationAngle = 90f;
    9.     public float RotationDeadzone = 0.1f;
    10.  
    11.     private Player _player;
    12.     private Transform _battleMap;
    13.     private Vector3 _rotateVector;
    14.  
    15.     void Awake ()
    16.     {
    17.         _player = ReInput.players.GetPlayer(PlayerID);
    18.         _battleMap = gameObject.GetComponent<Transform>();
    19.  
    20.     }
    21.    
    22.     void Update ()
    23.     {
    24.         GetInput();
    25.         ProcessInput();
    26.     }
    27.  
    28.     private void GetInput()
    29.     {
    30.         // Action ID = BattleMapControl/RotateMap
    31.         _rotateVector.y = _player.GetAxisRaw(0);
    32.     }
    33.  
    34.     private void ProcessInput()
    35.     {
    36.         if (_rotateVector.y > RotationDeadzone || _rotateVector.y < -RotationDeadzone)
    37.         {
    38.             _battleMap.rotation = Quaternion.AngleAxis(RotationAngle, Vector3.up);
    39.         }
    40.     }
    41. }
     
  2. NickAtUnity

    NickAtUnity

    Unity Technologies

    Joined:
    Sep 13, 2016
    Posts:
    84
    Your code is creating the Quaternion each time you update the rotation, hence your rotation will be the same. What you can do is multiply the rotation by the result of Quaternion.AngleAxis to rotate it (quaternions get multiplied together to "add" the result of their rotations). Alternatively the easier approach is to use Transform.Rotate instead:

    Code (CSharp):
    1. _battleMap.Rotate(Vector3.up, RotationAngle);
    This removes the need to worry about Quaternion math entirely and just makes your code a bit more readable.
     
  3. GerPronouncedGrr

    GerPronouncedGrr

    Joined:
    Mar 30, 2013
    Posts:
    25
    Ahhhhh... Seems so obvious when someone else points it out -_-

    Thanks very much, Nick!