# How to clamp rotation for orbit camera

I am trying to figure out how to clamp the Y axis for my 3rd person orbit camera. I never figured out how to clamp rotations properly, but here is my code in C#

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

public class CameraFollow : MonoBehaviour {

public float maxY, minY;

public Transform target;

public Vector3 offset;
public float turnSpeed = 4;

void Start(){

offset = new Vector3 (target.position.x, target.position.y + 8, target.position.z - 7);

}

void LateUpdate(){

offset = Quaternion.AngleAxis (Input.GetAxis ("RHorizontal") * turnSpeed, Vector3.up) * offset;

offset = Quaternion.AngleAxis (Input.GetAxis ("RVertical") * turnSpeed, Vector3.right) * offset;
offset.y = Mathf.Clamp (offset.y, minY, maxY);

transform.position = target.position + offset;
transform.LookAt (target.position);

}

}
``````

Thanks to anyone in advanced that helps me out

Here is one way of clamping a rotation.

``````// get axis angles
Vector3 euler = transform.rotation.eulerAngles;

// convert y axis to relative value
euler.y = ((euler.y + 180) % 360) -180;

// clamp value
euler.y = Mathf.Clamp(euler.y, -45, 45);

// apply clamp
transform.rotation = Quaternion.Euler(euler);
``````