Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Two RotateAround calls lead to 3-axis rotation

Discussion in 'Scripting' started by TxAg03, Oct 23, 2015.

  1. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    I'm trying to rotate a GameObject along 2 axes, X and Y, based on mouse movement, but when I do this, I get a 3-axis rotation.

    Code (CSharp):
    1. float horizontal = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
    2. float vertical = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;          
    3. myGameObject.transform.RotateAround(Vector3.zero, Vector3.up, -horizontal);
    4. myGameObject.transform.RotateAround(Vector3.zero, Vector3.right, vertical);
    If you only move the mouse along 1-axis, it works. But if you move the mouse along both axes, it ends up rotating on all 3 axes.

    My pivots are not always positioned at the center of the GameObject, so just rotating it will not work correctly.

    Any suggestions on how to only rotate around 2 axes with a GameObject that's pivot is not at its center?

    Thank you.
     
  2. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    One option, would be to create an empty game object to represent the center of rotation. You can child myGameObject to this "pivot point". Then, simply Rotating "pivot point" will also move and rotate the child object.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Read the documentation for 'Transform.RotateAround':
    http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

    If you first rotation something around the up axis, than right in world coordinates will not be the right of the object, but instead an axis off of the local right of your object by the amount of degrees you rotate around up.

    This would mean, locally, it'd be the something like <0.9, 0, 0.1> or so (not really being perfect with those numbers). Which is 2 axes.

    Try passing in the local up and right vectors of the transform:

    Code (csharp):
    1.  
    2. myGameObject.transform.RotateAround(Vector3.zero, myGameObject.transform.up, -horizontal);
    3. myGameObject.transform.RotateAround(Vector3.zero, myGameObject.transform.right, vertical);
    4.  
     
  4. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    lordofduct, thank you so much for pointing me in that direction. Using both GameObject directions gave me the exact same problem as before. But changing the UP direction to that of the GameObject and leaving the right alone fixed it.
     
  5. RelayRay

    RelayRay

    Joined:
    Mar 28, 2019
    Posts:
    2
    Sorry I don't get it. I have the exact same problem as before. Please explain :)
     
  6. Hector1503

    Hector1503

    Joined:
    Jun 19, 2019
    Posts:
    4
    Hi! The problem from what I understand is that the rotation done in the same space (local or global) are cumulative, so in order to do them separatedly we have to do each of them in one of the spaces.
    I found a pretty good explanation here:
    https://gamedev.stackexchange.com/q...-so-why-does-it-keep-twisting-around-the-thir

    Now the problem I'm facing is that when I try to do a rotation using the 3 axes there is also this problem between the 2 axes sharing the same space, and I don´t know how to fix this.
     
  7. FractalNinja

    FractalNinja

    Joined:
    Sep 22, 2019
    Posts:
    1
    I came across this thread trying to tackle the same issue, I ended up moving the Y axis in world space, and the X axis in local space. With the Y axis constrained to world space the cumulative effect was gone. The last two lines are the relevant ones. Hope this helps

    Code (CSharp):
    1. public class PodController : MonoBehaviour
    2. {
    3.     float _horizontalSpeed = 2.0f;
    4.     float _VerticalSpeed = 2.0f;
    5.  
    6.     void Update()
    7.     {
    8.         float h = _horizontalSpeed * Input.GetAxis("Mouse X");
    9.         float v = _VerticalSpeed * Input.GetAxis("Mouse Y");
    10.         transform.Rotate(0, h,0, Space.World);
    11.         transform.Rotate(v, 0, 0, Space.Self);
    12.    
    13.     }
    14. }
    15.  
     
  8. PolyNeXx

    PolyNeXx

    Joined:
    Jul 5, 2015
    Posts:
    1


    Have you found a way to fix this?