Rotate a plane upto a limit in C#

Hello
I am a beginer in Unity so please be Patient.
I am trying to rotate a plane upto 30 degrees in each direction using Vector3.up ,.down,.left and .right.
My z local Rotation Needs to be Zero all the time.
I tried to Limit the Rotation using clamping but does not seem to work.
It rotate over 30 degrees all the time.

I require some help so as to Limit my Rotation in each direction.

this is my code:

void Update ()
{

    Vector3 mirrorRotation = mirror.transform.localRotation.eulerAngles;
    mirrorRotation.z = 0;
    mirror.transform.rotation = Quaternion.Euler (mirrorRotation);

    Angle =    mirror.transform.eulerAngles - originMirror;
    Debug.Log ("Angle:" + (mirror.transform.eulerAngles - originMirror));

    



    if (thumbTipPosition.x >= originThumb.x) {
     

        mirror.transform.Rotate (Vector3.down * RotationSpeed);

        Debug.Log ("rotatedRight");
    } else if (thumbTipPosition.x <= originThumb.x) {
      

        mirror.transform.Rotate (Vector3.up * RotationSpeed);

        Debug.Log ("rotatedLeft");
    } 
    if (thumbTipPosition.y >= originThumb.y) {
       
        mirror.transform.Rotate (Vector3.right * RotationSpeed);

        Debug.Log ("rotatedUp");
    } else if (thumbTipPosition.y <= originThumb.y) {

        mirror.transform.Rotate (Vector3.left * RotationSpeed);

        Debug.Log ("rotatedDown");
    }

        clampedXValue = Mathf.Clamp (mirrorXvalue, -maxRotation, maxRotation);
        Debug.Log ("clampedX" + clampedXValue);

        clampedYValue = Mathf.Clamp (mirrorYvalue, -maxRotation, maxRotation);
        Debug.Log ("ClampedY" + clampedYValue);

    }

//
// void ClampAngleX()
// {
// if (mirrorXvalue > 30) {
// mirrorXvalue -= -0;
// }
// else if (mirrorXvalue < -30) {
// mirrorXvalue += 30;
// }
//
// mirrorXvalue = Mathf.Clamp (mirrorXvalue,-maxRotation,maxRotation);
// Debug.Log (“Xvalue:” + mirrorXvalue);
// }
}

Thank aton

You could do this…

Add this script to the ‘mirror’ GameObject:

PlaneRotation.cs

using UnityEngine;

public class PlaneRotation : MonoBehaviour
{
	public Vector3 minRotation = new Vector3(-30, -30, 0);
	public Vector3 maxRotation = new Vector3(30, 30, 0);

	public Vector3 currentRotation = Vector3.zero; // should not be set directly; use SetCurrentRotation()

	public void SetCurrentRotation(Vector3 euler)
	{
		euler.x = Mathf.Clamp(euler.x, minRotation.x, maxRotation.x);
		euler.y = Mathf.Clamp(euler.y, minRotation.y, maxRotation.y);
		euler.z = Mathf.Clamp(euler.z, minRotation.z, maxRotation.z);
		currentRotation = euler;
		transform.rotation = Quaternion.Euler(currentRotation);
	}
}

And rewrite your other script’s code like so:

    void Awake()
	{
		PlaneRotation pr = mirror.GetComponent<PlaneRotation>();
		pr.maxRotation = new Vector3(maxRotation, maxRotation, 0);
		pr.minRotation = new Vector3(-maxRotation, -maxRotation, 0);
	}

	void Update()
	{
		PlaneRotation pr = mirror.GetComponent<PlaneRotation>();
		Vector3 mirrorRotation = pr.currentRotation;

		float speed = RotationSpeed * Time.deltaTime;

		if (thumbTipPosition.x >= originThumb.x) {
			mirrorRotation.y += speed;
			Debug.Log("rotatedRight");
		}
		else if (thumbTipPosition.x <= originThumb.x) {
			mirrorRotation.y -= speed;
			Debug.Log("rotatedLeft");
		}

		if (thumbTipPosition.y >= originThumb.y) {
			mirrorRotation.x -= speed;
			Debug.Log("rotatedUp");
		}
		else if (thumbTipPosition.y <= originThumb.y) {
			mirrorRotation.x += speed;
			Debug.Log("rotatedDown");
		}

		pr.SetCurrentRotation(mirrorRotation);
	}