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!