Mathf.Clamp not working as expected with rotation values

Hey !

I’m having a annoying issue with Mathf.Clamp. The issue is that I can’t clamp the rotation between 330 and 30. This script below is attached to a gun held by a character. I want to basicly limit the rotation between 330 and 30. I’ve tried limiting the angle in between 30 and 330 but then the gun shoots backwards.

I hope you guys could help me out, and sorry about my shitty explanation.

void Update ()
		Vector3 mousePos = Input.mousePosition;
		mousePos.z = -(transform.position.x - Camera.main.transform.position.x);
		Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
		mousePos.x = mousePos.x - objectPos.x;
		mousePos.y = mousePos.y - objectPos.y;
		float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
		angle += 180f; //So the gun is directed towards the mouse
		/*if(angle < 330)
			transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
		//angle = Mathf.Clamp(angle, 330, 30);
		//angle = Mathf.Min(30);
		//angle = Mathf.Max (330);
		angle = ClampAngle (angle, 330, 30);
		//angle = Mathf.Clamp (angle, -100, 100);
		//transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
		Debug.Log (angle);
		transform.eulerAngles = new Vector3(0, 0, angle);

	public static float ClampAngle (float angle, float min, float max) {
		if (angle <  30.0f)
			angle += 30.0f;
		if (angle > 330.0f)
			angle -= 330.0f;
		//Debug.Log (angle);
		return Mathf.Clamp (angle, min, max);

Mathf.Clamp is documented as taking three values. The second is the min value and the third is the max value. Each time you use this API you have the min and max values reversed. I think we can all agree here that 330 is bigger than 30. I suspect what you really want is the angle to vary between -30 and +30. (Otherwise I think you’d say you want to limit between 30 and 330.) So you’d want to precondition the angle to be in the range -180…180 prior to calling Clamp(angle, -30.0f, 30.0f).