3D Model rotating weirdly, Opposite rotation when rotating 180 deg in x axis

I am a beginner and i am trying to rotate a 3D model by applying Rotate script that i wrote. The script works well in some scenarios for example when i rotate it from the front its working fine but when i rotate it left or right 180 degrees, and then rotate it up , it goes downward and if i rotate it down it goes up. in short from the front the rotation is working fine but from the back side of the model , the y axis rotation becomes opposite. When i start rotating the object left or right, the y axis rotation starts to behave weirdly.
This is my hierarchy : Game Object → Point → 3D Model.
The script i have written is applied on Point because if i apply it directly on the Game Object its not working. This is my rotate.cs script:

void Update()
{
    Debug.Log("Inside function Update");
    HandleRotationInput();
    HandleZoomInput();
}

void HandleRotationInput()
{
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && !isZooming)
    {
        float rotationX = -Input.GetTouch(0).deltaPosition.y * rotateSpeed * 
        Time.deltaTime; // Rotate around local x-axis
        float rotationZ = -Input.GetTouch(0).deltaPosition.x * rotateSpeed * 
        Time.deltaTime; // Rotate around local z-axis
        transform.Rotate(Vector3.left, rotationX, Space.Self); 
// Rotate around local x-axis
        transform.Rotate(Vector3.forward, rotationZ, Space.Self); 
// Rotate around local z-axis
    }
    else if (Input.GetMouseButton(0) && !isZooming)
    {
        float rotationX = Input.GetAxis("Mouse Y") * rotateSpeed; 
// Rotate around local x-axis
        float rotationZ = Input.GetAxis("Mouse X") * rotateSpeed; 
// Rotate around local z-axis
        transform.Rotate(Vector3.right, rotationX, Space.Self); 
// Rotate around local x-axis
        transform.Rotate(-Vector3.forward, rotationZ, Space.Self); 
// Rotate around local z-axis
    }
}

please help me, i am a beginner…

Have I understood you correctly, that you wish to rotate the model from the perspective of the camera the same way that the “Rotate Tool” functions in the Unity editor when clicking inside the sphere gizmo but not directly on one of the axis circles?

If so, you can do this:

void HandleRotationInput()
{
    // (...)

    Vector2 touchDelta = Input.GetTouch(0).deltaPosition * rotateSpeed * Time.deltaTime;

    // "transform" refers to the transform of the object to be rotated
    transform.Rotate(Camera.main.transform.up,   -touchDelta.x, Space.World);
    transform.Rotate(Camera.main.transform.right, touchDelta.y, Space.World);
}

If not, please be more specific :slight_smile:

EDIT:
I added the touchDelta assignment. It’s pretty much the same as yours. I used a Vector2 instead of two separate variables.

Adjust the sign (positive/negative) of touchDelta on the following lines.
Negative X and positive Y should mimic rotation in the Unity editor:

transform.Rotate(Camera.main.transform.up,   -touchDelta.x, Space.World);
transform.Rotate(Camera.main.transform.right, touchDelta.y, Space.World);

Rotation

Seems like the issue you’re facing might be related to the combination of rotations and the orientation of your 3D model. To address this, you can modify your rotation script to handle the rotations in a more consistent manner. Here’s an updated version of your script:

void Update()
{
    HandleRotationInput();
    HandleZoomInput();
}

void HandleRotationInput()
{
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && !isZooming)
    {
        float rotationX = -Input.GetTouch(0).deltaPosition.y * rotateSpeed * Time.deltaTime;
        float rotationY = Input.GetTouch(0).deltaPosition.x * rotateSpeed * Time.deltaTime;

        // Rotate around the world axes instead of local axes
        transform.Rotate(Vector3.right, rotationX, Space.World);
        transform.Rotate(Vector3.up, rotationY, Space.World);
    }
    else if (Input.GetMouseButton(0) && !isZooming)
    {
        float rotationX = Input.GetAxis("Mouse Y") * rotateSpeed;
        float rotationY = Input.GetAxis("Mouse X") * rotateSpeed;

        // Rotate around the world axes instead of local axes
        transform.Rotate(Vector3.right, rotationX, Space.World);
        transform.Rotate(Vector3.up, rotationY, Space.World);
    }
}

Changes made:

  1. Used Vector3.up for the Y-axis rotation in both touch and mouse input cases. This ensures that the rotation is consistent regardless of the model’s orientation.
  2. Changed Space.Self to Space.World for rotation around the X-axis to maintain consistency.

Try this updated script, and it should provide a more stable and consistent rotation experience for your 3D model. Adjust the rotateSpeed variable as needed for the desired rotation sensitivity. i hope this will help you

Yes you understood it right, i meant to rotate my model just like the rotate tool in unity, along with the code you provided, can you provide me the touchDelta.x and touchDelta.y formulas too , please?