Hi All,

I am a newbie to Unity and C# scripting. Am trying a render a 360 degree video in a sphere and should rotate the sphere horizontally. But vertically I should allow only 90 degrees because beyond certain degree the video will be inversed. When I try to rotate the sphere, all the axis are moving. This has to take place during MouseDrag event.

Event m_Event = Event.current;

if (m_Event.type == EventType.mouseDrag)

{

float roty = Input.GetAxis(“Mouse X”);

roty = roty * 10 * Mathf.Deg2Rad;

float rotx = Input.GetAxis(“Mouse Y”);

rotx = rotx * 10 * Mathf.Deg2Rad;

if (Mathf.Abs(roty) > Mathf.Abs(rotx))

{

transform.RotateAround(Vector3.up, roty); // Horizontal movement of sphere

}

if (Mathf.Abs(roty) < Mathf.Abs(rotx))

{

transform.RotateAround(Vector3.right,rotx); //Vertical movement of sphere

}

}

I think you got issue with ordering of rotation axis. And there possibly confusion of local vs global axis reference.

I havent used transform.RotateAround (), but I suppose, it you want to use

transform.RotateAround ( Vector3.up, roty ) ; // I think is global rotation reference (Up)

transform.RotateAround ( transform.right, rotx ) ; // which uses local rotation references. (Right)

But generally I would approach this different way.

use rot x and y to store rotation, rather than delta rotaion

Then use Quaternion to rotate.

transform.rotation = Quaternion.euler ( 0, roty, 0 ) ; // first rotate horizontally

transform.rotation *= Quaternion.euler ( rotx, 0, 0 ) ; // then rotate vertically, with local x axis

You can narrow it down to

transform.rotation = Quaternion.Euler ( 0, roty, 0 ) * Quaternion.Euler ( rotx, 0, 0 ) ;

So this ensures rotation axis order.

1 Like

Thank you so much. Its working. Am able to rotate the sphere both horizontally and vertically. But How to make vertical rotation limit to 90 degree. If I scroll with the mouse after reaching 90 degree, I should not be able to scroll up. Similarly if I scroll the mouse down after reaching -90 degree, I should not be able to scroll down.