- Home /
Question by
Zeke-27 · Sep 09, 2019 at 05:12 AM ·
rotationrotation axisclampclamped rotation
How to clamp character rotation on Y and X axis?
I've got a simple script for rotating a character with MouseDrag but I'm not sure how to apply limits to their rotation. I know how to clamp movement but rotation seems to be a little more complicated. Anyone know how to do this?
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
Comment
Best Answer
Answer by Laiken · Sep 09, 2019 at 05:40 PM
Is this what you are looking for?
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
Vector3 myRotation = transform.rotation.eulerAngles;
myRotation.x = Mathf.Clamp(myRotation.x, minValue, maxValue);
myRotation.y = Mathf.Clamp(myRotation.y, minValue2, maxValue2);
transform.rotation = Quaternion.Euler(myRotation);
}