Is there a way to lock my camera's rotation and movement on certain axis?

I have my camera attached to an object in my scene and it is parented to it. Therefore my camera follows the object in all directions and rotations. however I don’t want this. I want my camera to be locked on the y position axis, and the x, and z rotation axis. Using c#.

Parent an empty to the object, not the camera itself, and in Update set the camera to use that empty’s x and z positions.

[SerializeField] Transform target;

void Update () {
    transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); 
}

The rotation is a bit more difficult, but you can calculate the necessary angle with a atan over the quotiont of the difference in the remaining axes.

e.g. if you want to only rotate along y, then the angle would be

float angle = Mathf.Atan((target.position.x - transform.position.x)/(target.position.z - transform.position.z))      //Possibly wrong sign, depending on your setup

transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, angle, transform.rotation.eulerAngles.z);

Alternatively, you can use a lookRotation to create a rotation to look at your object. If the position of the camera is fixed as above, this should only rotate along the remaing axis.

transform.rotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);

Hi,

Add a rigidbody to your cam, uncheck the ‘Use Gravity’ option unless what it will fall. Then, in ‘Constraints’, check all the axes (translation and/or rotation) you want to freeze :slight_smile: