So basically I made an object rotate when I click and drag it but I want to limit the rotation to (50, -50) on the X axis, and (30, -30) on the Y axis. I tried a lot of things but nothing seems to work so far…
Here’s my code:
private void OnMouseDrag()
{
float XaxisRotation = Input.GetAxis(“Mouse X”) * rotationSpeed;
float YaxisRotation = Input.GetAxis(“Mouse Y”) * rotationSpeed;
transform.Rotate(Vector3.down, -XaxisRotation, Space.Self);
transform.Rotate(Vector3.right, -YaxisRotation, Space.Self);
}
(I will appreciate it if you help me.)
(If you can’t, is there a better way to rotate the object?)
Yep, there is. Track your own X and Y rotations - that is, add your GetAxis results to a class-level variable. Then use transform.rotation = Quaternion.Euler(y,x,0) to set it. Once you’ve got that, you can easily Mathf.Clamp your tracked rotation values to anything you like.
I FINALLY DID IT WITHOUT CHANGING TOO MUCH ON MY CODE!!! I found a tutorial by Brackeys on how to do fps movement. Here’s the final code if anyone’s interested…