# Transform.rotate limits

Hey.
Ther’s a way to limit the angles of rotation with transform.rotate? Can someone suggest me an example? I tried this but not work. I also tried to find some old question in unity-answers but nothing.
Thanks a lot.

``````public GameObject[] arm;

float speedUp;
float speedDown;
Vector3 StartPosition;

void Awake()
{
speedUp = 80f;
speedDown = 60f;
}

// Update is called once per frame
void Update () {

//go up
if (Input.GetKey (KeyCode.Z)) {

arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speedUp);

if(transform.Rotate > 50) //rotation limits
{
transform.Rotate = 50;

}

}

//go down
if (Input.GetKey (KeyCode.X)) {

arm[0].transform.Rotate(-Vector3.right * Time.deltaTime*speedDown);

}
}
``````

Well, try this, It’s easy to understand as well.

(Declaration)
public float rotationZ;
public float sensitivityZ = 5;

``````		void Start()
{
transform.rotation = Quaternion.identity;
}

void Update()
{
Debug.Log(transform.rotation.eulerAngles.z);
LockedRotation();
}

void LockedRotation()
{
rotationZ += Input.GetAxis("Mouse ScrollWheel") * sensitivityZ * 10;
rotationZ = Mathf.Clamp(rotationZ, -45.0f, 45.0f);

transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ);
}
``````

I haven’t tried it yet, but I believe:

transform.localRotation.x = Mathf.Clamp(transform.localRotation.x, Max, Min);

where the transform.localRotation.x in the brackets is your current value, then it restricts it between your max and min values. You can set them with floats or integers.

Correct me if I’m wrong though, I myself am trying to get a local rotation restriction going too

1. Check out this function.

2. Search old questions.

3. Look in MouseLook script in Standard Assets, it has this implemented