rotate within specific angle

Hi, i am new in Unity. As the code below, I knew how to rotate object freely and control it’s speed. Mine goal now is confine rotation within some specific angle, for example 40 to 180 degree. I get some idea from searching know that function Math.Clamp or .eulerAngles might be the solution to solved it. However, I don’t know how to combine those function within code. Please assist me or give me some hint. Thanks!

using UnityEngine;
using System.Collections;

public class rotateObj_x : MonoBehaviour 
{
	
	private float sensitivity;
	private Vector3 mouseReference;
	private Vector3 mouseOffset;
	private Vector3 rotation;
	private bool isRotating;
	
	void Start ()
	{
		sensitivity = 1f;
		rotation = Vector3.zero;
	}
	
	void Update()
	{
		if(_isRotating)
		{
			mouseOffset = (Input.mousePosition - mouseReference);
			rotation.x = -(mouseOffset.x + mouseOffset.x) *sensitivity;
			transform.Rotate(rotation);
			mouseReference = Input.mousePosition;
		}
	}
	void OnMouseDown()
	{
		isRotating = true;
		mouseReference = Input.mousePosition;
	}
	
	void OnMouseUp()
	{
		isRotating = false;
	}
}

Hi,

One solution would be to force the rotation to its max or min value when you’re out of range. Since you need it all the time, you need to write it in Update().

Code would look like this (that’s not true C#, you need to rework it a bit - transform.rotation is a Quaternion so you need to transform vector3 to quat):

if (transfom.rotation.x > 180) {
   transform.rotation = Vector3(180, transform.rotation.y, transform.rotation.z);
} else if (transfom.rotation.x < 40) {
   transform.rotation = Vector3(40, /* ... */);
}