- Home /
How to restrict rotation properly
How do I restrict rotation angles? The rotation just keeps slipping through the limit and stopping forever. When I set 355 as a limit, it stops at 354.993 or something. Can something be done about it? Is the problem with Update frequency?
I am using this code:
if(Input.GetAxis("Mouse X")<0)
{
if ((transform.rotation.eulerAngles.y < 4) || (transform.rotation.eulerAngles.y > 355))
{
transform.RotateAround(cameraFocus.position, Vector3(0, -1, 0), 5 * Time.deltaTime);
print(transform.rotation.eulerAngles.y);
}
}
//right
if(Input.GetAxis("Mouse X")>0)
{
if ((transform.rotation.eulerAngles.y < 4) || (transform.rotation.eulerAngles.y > 355))
{
transform.RotateAround(cameraFocus.position, Vector3.up, 5 * Time.deltaTime);
print(transform.rotation.eulerAngles.y);
}
}
I need this for synching cursor movement with camera rotation, obviously.
Please, help.
Answer by Winterblood · Jan 02, 2012 at 03:37 AM
Not clear what this code is trying to do. It looks like you're trying to limit the camera rotation to +/- 5 degrees, which seems a bit small.
Personally, I'd insert a dummy GameObject which sits at the cameraFocus position, with the camera parented to that. Then you just set the dummy's transform.rotation.eulerAngles, rather than doing a RotateAround which will gradually accumulate floating point error. You can then easily clamp the Y rotation to exactly what you want.
How do I add those eulerAngles then? What code would you use for that?
Pretty much what you've got, except you want the rotation script to be on the dummy pivot object ins$$anonymous$$d of the camera. Try this (may have typos, I write in C# usually):
var maxRot = 45;
...
var angle = transform.rotation.eulerAngles.y;
angle += Input.GetAxis("$$anonymous$$ouse X");
if (angle > maxRot)
angle = maxRot;
else if (angle < -maxRot)
angle = -maxRot;
transform.rotation.eulerAngles = new Vector3( 0, angle, 0 );
NB. You have to set the entire Vector3 - you can't modify transform.rotation.eulerAngles.y on it's own.
Answer by pset · Jul 25, 2017 at 02:36 PM
You can use Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta) function to restrict rotation:
Quaternion ClampRotationXByLookDirection(Quaternion curentRotation)
{
Vector3 lookDirectionX = Vector3.ProjectOnPlane(lookDirection, Vector3.right).normalized;
Quaternion lookRotation = Quaternion.LookRotation(lookDirectionX);
Quaternion towardsRotation= Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaX);
return towardsRotation;
}
Quaternion ClampRotationYByLookDirection(Quaternion curentRotation)
{
Vector3 lookDirectionY = Vector3.ProjectOnPlane(lookDirection, Vector3.up).normalized;
Quaternion lookRotation = Quaternion.LookRotation(lookDirectionY);
Quaternion towardsRotation= Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaY);
return towardsRotation;
}
Your answer
Follow this Question
Related Questions
Edited mouselook script rotation clamp not working 0 Answers
audiosource.PlayClipAtPoint only plays once 2 Answers
Quaternion - Euler Angles, weird variable swapping when converting 2 Answers
My rigidbody's y rotation is always 180 1 Answer
Camera rotate around a moving object: fixedLateUpdate? 1 Answer