Two RotateAround calls lead to 3-axis rotation

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.

float horizontal = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
float vertical = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;          
myGameObject.transform.RotateAround(Vector3.zero, Vector3.up, -horizontal);
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.

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.

Read the documentation for ‘Transform.RotateAround’:

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:

myGameObject.transform.RotateAround(Vector3.zero, myGameObject.transform.up, -horizontal);
myGameObject.transform.RotateAround(Vector3.zero, myGameObject.transform.right, vertical);
1 Like

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.

Sorry I don’t get it. I have the exact same problem as before. Please explain :slight_smile:

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/questions/136174/im-rotating-an-object-on-two-axes-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.

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

public class PodController : MonoBehaviour
{
    float _horizontalSpeed = 2.0f;
    float _VerticalSpeed = 2.0f;

    void Update()
    {
        float h = _horizontalSpeed * Input.GetAxis("Mouse X");
        float v = _VerticalSpeed * Input.GetAxis("Mouse Y");
        transform.Rotate(0, h,0, Space.World);
        transform.Rotate(v, 0, 0, Space.Self);
    
    }
}

Have you found a way to fix this?