Can't rotate camera because of MouseLook.cs

So I’m using the FPS Controller that comes with Unity3D and when I try to change the main camera’s rotation, I can’t do it because MouseLook.cs gets the rotation from where your Mouse is positioned.

How can I make it so I can also change the rotation with code, not just with the mouse position?

Here’s the MouseLook.cs:

    using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
///   -> A CharacterMotor and a CharacterController component will be automatically added.

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
 public RotationAxes axes = RotationAxes.MouseXAndY;
 public float sensitivityX = 15F;
 public float sensitivityY = 15F;

 public float minimumX = -360F;
 public float maximumX = 360F;

 public float minimumY = -60F;
 public float maximumY = 60F;

 float rotationY = 0F;

 void Update ()
 {
 if (axes == RotationAxes.MouseXAndY)
 {
 float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
 
 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
 
 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
 }
 else if (axes == RotationAxes.MouseX)
 {
 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
 }
 else
 {
 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
 
 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
 }
 }
 
 void Start ()
 {
 // Make the rigid body not change rotation
 if (rigidbody)
 rigidbody.freezeRotation = true;
 }
}

I know this is old but the current answers are not the most efficient.

The main issue is that the MouseLook script stores the “Y” rotation (which is actually localEulerAngles.x) internally. So when you change the rotation manually, that rotation is basically ignored by MouseLook.

To fix this, every time you change the rotation manually, you need to manually set rotationY, like this:

rotationY = -transform.localEulerAngles.x;

If you are trying to set the camera’s initial rotation, set it in Awake and then add the code above to the MouseLook’s Start method.

To simplify things, you can add the following method to the MouseLook class, and then call it every time you manually change the rotation.

public void ResetYRotation()
{
    rotationY = -transform.localEulerAngles.x;
}

The easiest way would be to disable the MouseLook component when you want your other camera script to take over. On your character in the Update method, do something like:

   GetComponent<MouseLook>().enabled = false;

Then set it back to enabled when you want MouseLook to take over again.

Actually I got it working thanks to another post

You have to destroy Mouselook Script

Destroy(gameObject.GetComponent(“MouseLook”));
then transform.rotation
then
gameObject.AddComponent(“MouseLook”);