# Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag()

Hello guys
I’ve been struggling in rotating a gameobject on y and z axis only with Y value clamped between (-90 & -120) & Z value between (0 & -10).
I’ve tried many online solutions with eulerangles and rotation and still stuck.
I want to rotate 3D model with drag so I thought that OnMouseDrag would work.
Any help would be really appreciated. Thanks.

test this code, is not optimised but it should work ALSO take into account this, dont work with euler angles with negative values, since for unity -110 is 250 so you will have a lot of issues comparing values (you will probably need to use the positive values)

``````    void Update()
{
if (Input.GetMouseButtonDown(0)) lastPosition = Input.mousePosition;
}

Vector2 lastPosition;
public float speed = 1f;
private void OnMouseDrag()
{
Debug.Log(transform.rotation.eulerAngles.y);

if (Input.mousePosition.x > lastPosition.x + 10)
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x,Mathf.Clamp( transform.rotation.eulerAngles.y + speed, 0, 30), transform.rotation.eulerAngles.z);
else if(Input.mousePosition.x < lastPosition.x - 10)
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y - speed, 0, 30), transform.rotation.eulerAngles.z);

if (Input.mousePosition.y > lastPosition.y + 10)
transform.rotation = Quaternion.Euler(Mathf.Clamp(transform.rotation.eulerAngles.x + speed, 0, 10), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
else if (Input.mousePosition.y < lastPosition.y - 10)
transform.rotation = Quaternion.Euler(Mathf.Clamp(transform.rotation.eulerAngles.x - speed, 0, 10), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
}
``````

I changed your code a little bit according to my values but there is still some error in Z axis.
Its quickly snaps between the values used in code but tha’s not the case with Y axis. The y axis is working perfectly.
My 3D model is set as (0,270,360) and it is supposed to go to (0,230,340).
Here is the code I changed

``````Vector2 lastPosition;
public float speed = 1f;

private void OnMouseDrag()
{
//Debug.Log(transform.rotation.eulerAngles.y);

if (Input.mousePosition.x < lastPosition.x + 10)
{
print("condition 1");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y + speed, 230, 270), transform.rotation.eulerAngles.z);
}

else if (Input.mousePosition.x > lastPosition.x - 10)
{
print("condition 2");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y - speed, 230, 270), transform.rotation.eulerAngles.z);
}

if (Input.mousePosition.y > lastPosition.y + 10)
{
print("condition 3");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Clamp(transform.rotation.eulerAngles.z - speed, 340, 360));//org
}

else if (Input.mousePosition.y < lastPosition.y - 10)
{
print("condition 4");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Clamp(transform.rotation.eulerAngles.z - speed, 360, 340));//org
}
}
void OnMouseUp()
{
transform.rotation = Quaternion.Euler(0, 270, 360);
}
``````