Limiting rotation of an object

Hi,

I'm making a little game where you control a bunker on the ground defending itself from bombers and paratroopers. The bunker has a cannon which the player can rotate to aim at her enemies. My problem is that there is no limit to the rotation, so right now the player can aim the cannon straight down into the ground. What I would like to do is to limit the possible rotation between the values of, say, 0 to 180 along the x-axis.

So how can I limit the rotation values for an object?

You can use the same code from MouseLook.cs. The following code will lock rotation between -90 and 90 degrees in the Z-axis.

Here is an example:

// gobal 
private float rotationZ = 0f;
private float sensitivityZ = 2f;

void lockedRotation()
{
   rotationZ += Input.GetAxis("Mouse X") * sensitivityZ;
   rotationZ = Mathf.Clamp (rotationZ, -90, 90);
			
    transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ);
}

You can limit the rotation of an object like this, for example if you wanted to limit the X-rotation of an object between 30 and 150 degrees:

float MinClamp = 30;
float MaxClamp = 150;

// Other movement needs to be calculated first, so
// we use LateUpdate() instead of Update().
void LateUpdate()
{
     transform.rotation.eulerAngles = new Vector3(
          Mathf.Clamp(transform.rotation.eulerAngles.x, MinClamp, MaxClamp),
          transform.rotation.eulerAngles.y,
          transform.rotation.eulerAngles.z
     );
}

You can obviously modify this to clamp any angle and any value you want, and it can be in its own script file, you don't have to include it in another script.

You can use Quaternion.Slerp() to limit the rotation of an object, for instance:

transform.localRotation = Quaternion.Slerp(rot, Quaternion.Euler(Vector3.forward), limit);

This would blend between Quaternion rot and forward based on limit.

You can use quaternion function
Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta)
to restrict rotation
like this:

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;
}

This limits how far from vertical the Y-axis of an object’s Transform can get. You set yLimit to the cosine of the maximum angle (here, about 45 degrees).

using UnityEngine;

public class RollLimiter : MonoBehaviour
{
    public float yLimit = .707f;

    Quaternion prior;

    void Start()
    {
        prior = transform.rotation;
    }

    void Update()
    {
        if ((transform.rotation * Vector3.up).y < yLimit)
        {
            transform.rotation = prior;
        }

        prior = transform.rotation;
    }
}

Take a look at the MouseLook script in Standard Assets for a way of limiting rotation.