- Home /

# 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.

**Answer** by xxmariofer
·
May 29 at 08:55 AM

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);
}
```

**Answer** by OGNE
·
May 29 at 11:05 AM

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);
}
```

it is not working because you are doing **"- speed"** in both if/elses you need to change it to + speed

only change to + speed in the condition3, and change in the condition 4 the 340 to be before the 360 (since the first value is the min and the second the max)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Smooth rotation to child object 1 Answer

Rotating local transform not rotating fully if tranform.up is not exactly at (0,1,0) 1 Answer

Rotation problem while dynamically changing animations 2 Answers

Quaternion distorts my object 1 Answer

Bizzare problem with limiting camera veritcal rotation 2 Answers