- Home /
How can I limit my rotation around an object ?
I've searched for the answer on the forums, but since I couldn't find one that works for me I'll ask again. So I have a camera rotating around an object. I want to limit the rotation to stop when it reaches X degrees of 90 or -90 aka when it looks at it exactly from above or below. I tried to use clamp, tried to set the degrees = 90 with eulerAngles if its above 90 or below -90 and both did not work, it's possible I didn't implement them properly.(Mind that, I clamped at the Start() function shouldn't that work ? ) It's like the camera completely ignores the limit. TL:DR I want to restrict the camera's rotation around an object but Mathf.Clamp and eulerAngles didn't work . Any guesses ? My code looks like this (Start function, variable delcaration etc are skipped) :
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
if (horizontalInput != 0)
{
transform.RotateAround(center.position, center.up, -horizontalInput * 90 * Time.deltaTime);
}
if (verticalInput != 0)
{
transform.RotateAround(center.position, transform.right, verticalInput * 90 * Time.deltaTime);
}
}
Clamping rotation in the Start function won't work, as it will clamp for the first frame and then leave the rest un-clamped. The thing making it difficult in this place is the fact that you are using RotateAround(). Ordinarily, to clamp the rotation you would have to have a separate variable to clamp which is then calculated into a Quaternion from EulerAngles. The easiest way to do this with RotateAround()-style rotation would be to set up your own rotation system.
Aaand how should I go about doing that ? I'm relatively new to unity and I still find it hard to understand how some things are supposed to go.
JScotty's answers shows a pretty good system that uses RotateAround, so I'd suggest following that.