GameObject wont rotate Y related to world space after limitation
After days off brainstorming its time to ask for help as this gave me headache.
So this is a situation: I have GameObject that I want to rotate its X and Y axis using mouse/touch. Its all fine when I use transform.Rotate and Space.World, GameObject rotates like it should. For example, I rotate it from left to right, than up and down, GameObject always rotate Y axis facing camera (like I wanted). Situation changes when I want to limit Y rotation (for example from -80 to 80). GameObject rotates from left to right just fine but for up and down GameObject always moving from one position. I will provide gifs so you can understand me clearly.
Script that I used when its working fine without limits:
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.Rotate(Vector3.up, -rotX, Space.World);
transform.Rotate(Vector3.right, -rotY, Space.World);
}
Script that limits:
float rotSpeed = 200;
private float rotX = 0f;
private float rotY = 0f;
private float rotXT = 0f;
void OnMouseDrag()
{
rotX -= Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
rotY += Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
rotY = Mathf.Clamp(rotY, -80, 80);
transform.rotation = Quaternion.Euler(-rotY, rotX, 0);
}
Please help guys!
Your answer
![](https://koobas.hobune.stream/wayback/20220612154609im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating on other axis 1 Answer
how to rotate a mesh with GetMouseButton with specific rotation value 2 Answers
Get angle of rotation around local axis 1 Answer
rotate an object with HTC Vive controller 0 Answers
How come when I rotate my spaceship, it also rotates on the two other axis's? Please help? 0 Answers