Want to freeze camera rotation from another script

this is my camera script how can i make it so camera doesnt move when ui opens but i can do things with my cursor

public class PlayerCam : MonoBehaviour
{

    public float sensX;
    public float sensY;

    public Transform orientation;
    Transform parent;
    Transform cam;
    float xRotation;
    float yRotation;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Update()
    {
        // get mouse input
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

        yRotation += mouseX;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        orientation.rotation = Quaternion.Euler(0, yRotation, 0);
        // rotate cam and orientation


    }

}

Heyyo!

You could try this:

  • Attach a new script onto your pause GameObject
  • Set the script up to have it accept a camera object/GameObject (public Camera cam; or something like that)
  • Disable the rotation of your camera in that script
  • In the inspector, pass the camera to your script by dragging your camera into the slot

This should be a setup that you can use!

My advice would be to make the UI component register itself whenever it wants to block the camera , that way you use this whenever multiple things at the same time want to block as well (pause menu , open inventory , etc …).

Something like this

// dummy interface to mark rotation blockers
public interface ICameraRotationFreezer { }

// Example of some UI that needs to block the camera rotation
public class SomeUI : MonoBehaviour , ICameraRotationFreezer
{
    public void Open()
    {
           PlayerCam cam; // somehow get a reference to the camera
           cam.Register(this);
    }
    public void Close()
    {
           PlayerCam cam; // somehow get a reference to the camera
           cam.Unregister(this);
    }
}
public class PlayerCam : MonoBehaviour
{

    public float sensX;
    public float sensY;

    public Transform orientation;
    Transform parent;
    Transform cam;
    float xRotation;
    float yRotation;
    
    List<ICameraRotationFreezer> rotFreezers = new();
    public void Register(ICameraRotationFreezer freezer)
    {
        rotFreezers.Add(freezer);
    }

    public void Unregister(ICameraRotationFreezer freezer)
    {
        rotFreezers.Remove(freezer);
    }

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Update()
    {
        if(rotFreezers.Count != 0)
        {
        // get mouse input
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

        yRotation += mouseX;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        }

        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        orientation.rotation = Quaternion.Euler(0, yRotation, 0);
        // rotate cam and orientation


    }

}