# Add max angle rotation

Hey… I’ve no idea how add a max angle to this script… because so the rotation is at 360°
i would make this:

``````#pragma strict

function Update () {

if(Input.GetKey(KeyCode.Z))
{
transform.Rotate (2.5, 0, 0 * Time.deltaTime);
}

if(Input.GetKey(KeyCode.X))
{
transform.Rotate (-2, 0, 0 * Time.deltaTime);
}

}
``````

thank you in advance

Do something along those lines :

``````if( transform.eulerAngles.x < min )
transform.rotation = Quaternion.Euler(min, 0, 0);
else if( transform.eulerAngles.x > max )
transform.rotation = Quaternion.Euler(max, 0, 0);
``````

You’ll probably have to do something a bit more clever if the angle goes around 0 or 360.

Hi @PaxStyle eulerAngles dont give a full 360° result so let’s check this example.

1- Camera Rotation must be (xxx,0,0) in the inspector or eulerAngles Y and Z will change and functions won’t work.

2- This only works with 1st and 3rd person cameras. Must be changed for other ones.

3- Camera must only rotate on X-Axis. Y and Z axis need to rotate in a parent to avoid values go crazy.

Now check real eulerAngles results:

Now let’s code functions that converts eulerAngle to full 0°-360° range and sets foward view from 0° to 180° and back to >180° to 360°.

``````public float EulerXAxisTo360(Vector3 eulerAngle)
{
float tmp = eulerAngle.x;
if (eulerAngle.x >=0f && eulerAngle.x <=90f)
{
if  (Mathf.Round(eulerAngle.y)==0)
{
tmp = eulerAngle.x;
}else
{
tmp = 180f - eulerAngle.x;
}
}else if(eulerAngle.x >= 270f && eulerAngle.x <= 360f)
{
if (eulerAngle.y == 180f)
{
tmp = 360 - eulerAngle.x + 180f;
}
else
{
tmp = eulerAngle.x;
}
}
tmp += 90;

//AVOID GOING OUTSIDE 0-360 RANGE
if (tmp < 0) tmp = 360 - tmp;
if (tmp >= 360) tmp -= 360f;

return tmp;
}
``````

Now let’s do it backwards. Note that CurrentY and CurrentZ are always camera’s transform.eulerAngles.y and transform.eulerAngles.z :

``````public Vector3 Full360toEulerXAxis(float Angle,float CurrentY, float CurrentZ)
{
Vector3 tmp = new Vector3(0, 0, 0);
float eulerXAngle=Angle;

if (Angle>=0 && Angle<90)
{
eulerXAngle += 270;

}else if (Angle>=90 && Angle<180)
{
eulerXAngle -= 90;
}else if (Angle>=180 && Angle<270)
{
eulerXAngle = 360 - eulerXAngle - 90 ;
}else if (Angle >= 270 && Angle <= 360 )
{
eulerXAngle = 360 - eulerXAngle + 270;
}
//AVOID GOING OUTSIDE 0-360 RANGE
if (eulerXAngle < 0) eulerXAngle = 360 - eulerXAngle;
if (eulerXAngle >= 360) eulerXAngle -= 360f;

tmp = new Vector3(eulerXAngle, CurrentY, CurrentZ);
return tmp;
}
``````

Finally you can clamp angles, looking you graphic lets say that you want to be able to see between 45° and 135° for example:

``````private float CameraXAngle;
public float RotationSpeed =2.5f;
public float MinAngle = 45f;
public float MaxAngle = 135f;

void Update()
{

if (Input.GetKey(KeyCode.Z))
{
transform.Rotate(RotationSpeed, 0, 0 * Time.deltaTime);
}

if (Input.GetKey(KeyCode.X))
{
transform.Rotate(-RotationSpeed, 0, 0 * Time.deltaTime);
}
CameraXAngle = transform.eulerAngles.x;
if (EulerXAxisTo360(transform.eulerAngles)< MinAngle)
{
transform.eulerAngles = Full360toEulerXAxis(MinAngle, transform.eulerAngles.y, transform.eulerAngles.z);
}else if (EulerXAxisTo360(transform.eulerAngles)> MaxAngle)
{
transform.eulerAngles = Full360toEulerXAxis(MaxAngle, transform.eulerAngles.y, transform.eulerAngles.z);
}

}
``````

You can check all the values with OnGui:

``````void OnGUI()
{
GUI.Label(new Rect(0, 0, 200, 200), "Full 360°=" + EulerXAxisTo360(transform.eulerAngles).ToString());
GUI.Label(new Rect(0, 20, 200, 200), "EulerAngles=" + transform.eulerAngles.ToString());
GUI.Label(new Rect(0, 40, 500, 200), "360°toEuler=" + Full360toEulerXAxis(EulerXAxisTo360(transform.eulerAngles), transform.eulerAngles.y, transform.eulerAngles.z).ToString());
}
``````

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

``````public class CAMTEST : MonoBehaviour
{
public float RotationSpeed = 10f;
public float MinAngle = 10f; //CAN'T GO BELOW ROTATION SPEED
public float MaxAngle = 180f;
void Update()
{

if (Input.GetKey(KeyCode.Z))
{
transform.Rotate(RotationSpeed, 0,  0);
}

if (Input.GetKey(KeyCode.X))
{
transform.Rotate(-RotationSpeed, 0, 0);
}

if (MinAngle < RotationSpeed) MinAngle = RotationSpeed;

if (EulerXAxisTo360(transform.eulerAngles)<= MinAngle)
{
transform.eulerAngles = Full360toEulerXAxis(MinAngle, transform.eulerAngles.y, transform.eulerAngles.z);
}else if (EulerXAxisTo360(transform.eulerAngles)>= MaxAngle)
{
transform.eulerAngles = Full360toEulerXAxis(MaxAngle, transform.eulerAngles.y, transform.eulerAngles.z);
}

}
public Vector3 Full360toEulerXAxis(float Angle,float CurrentY, float CurrentZ)
{
Vector3 tmp = new Vector3(0, 0, 0);
float eulerXAngle=Angle;

if (Angle>=0 && Angle<90)
{
eulerXAngle += 270;

}else if (Angle>=90 && Angle<180)
{
eulerXAngle -= 90;
}else if (Angle>=180 && Angle<270)
{
eulerXAngle = 360 - eulerXAngle - 90 ;
}else if (Angle >= 270 && Angle <= 360 )
{
eulerXAngle = 360 - eulerXAngle + 270;
}
//AVOID GOING OUTSIDE 0-360 RANGE
if (eulerXAngle < 0) eulerXAngle = 360 - eulerXAngle;
if (eulerXAngle >= 360) eulerXAngle -= 360f;

tmp = new Vector3(eulerXAngle, CurrentY, CurrentZ);
return tmp;
}
public float EulerXAxisTo360(Vector3 eulerAngle)
{
float tmp = eulerAngle.x;
if (eulerAngle.x >=0f && eulerAngle.x <=90f)
{
if  (Mathf.Round(eulerAngle.y)==0)
{
tmp = eulerAngle.x;
}else
{
tmp = 180f - eulerAngle.x;
}
}else if(eulerAngle.x >= 270f && eulerAngle.x <= 360f)
{
if (eulerAngle.y == 180f)
{
tmp = 360 - eulerAngle.x + 180f;
}
else
{
tmp = eulerAngle.x;
}
}
tmp += 90;

//AVOID GOING OUTSIDE 0-360 RANGE
if (tmp < 0) tmp = 360 - tmp;
if (tmp >= 360) tmp -= 360f;

return tmp;

}
void OnGUI()
{
GUI.Label(new Rect(0, 0, 200, 200), "Full 360°=" + EulerXAxisTo360(transform.eulerAngles).ToString());
GUI.Label(new Rect(0, 20, 200, 200), "EulerAngles=" + transform.eulerAngles.ToString());
GUI.Label(new Rect(0, 40, 500, 200), "360°toEuler=" + Full360toEulerXAxis(EulerXAxisTo360(transform.eulerAngles), transform.eulerAngles.y, transform.eulerAngles.z).ToString());
}

}
``````