How do I get the camera to change using triggers and clicks?

Hello, I’m making an investigation and puzzle game where the camera is swapped to fix on a certain point. I’m trying to make it work so that if you right click while the character collides with the trigger of that zone the camera will swap, but I don’t know how to do the script programming the collisions and the inputs together
Thank you very much for any help you can give me

public class GameCamera : MonoBehaviour
{
    public BoxCollider playerCollider;
    public GameObject cameraPlayer;
    public GameObject camera1;
    public GameObject camera2;
    public BoxCollider cameracollider1;


    void OnTriggerEnter(Collision playerCollider)
    {
        if (cameracollider1.;)
    }
        }

It might be easier to use trigger zones here. You can put a component on your player that references both their main camera, and the camera of the zone they entered.

It might look like this:

public class CameraZone : Monobehaviour
{
    private Camera zoneCamera;
  
    private void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out PlayerCameraManager cameraManager))
        {
            cameraManager.RegisterSwapCamera(zoneCamera);
        }
    }
  
    private void OnTriggerExit(Collider other)
    {
        if (other.TryGetComponent(out PlayerCameraManager cameraManager))
        {
            cameraManager.DeregisterSwapCamera(zoneCamera);
        }
    }
}

//put this on your player
public class PlayerCameraManager : Monobehaviour
{
    public Camera PlayerCamera; //this is your main camera, assign in inspector
  
    private Camera swapCamera;
  
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && swapCamera != null)
        {
            PlayerCamera.enabled = !PlayerCamera.enabled;
            swapCamera.enabled = !swapCamera.enabled;
        }
    }
  
    public void RegisterSwapCamera(Camera camera)
    {
        Camera previousCamera = swapCamera;
        swapCamera = camera;
      
        if (previousCamera != null) // do we already have a camera?
        {
            bool wasEnabled = previousCamera.enabled;
          
            if (wasEnabled)
            {
                previousCamera.enabled = false;
                swapCamera.enabled = true;
            }
        }
    }
  
    public void DeregisterSwapCamera(Camera camera)
    {
        if (swapCamera == camera)
        {
            if (swapCamera.enabled == true)
            {
                swapCamera.enabled = false;
                PlayerCamera.enabled = true;
            }
          
            swapCamera = null;
        }
    }
}

Written in notepad++, so potential errors present. But we’re giving ourselves a little API to allow other systems to register another camera that you can swap between.

It could also be edited to work with any number of camera (that’s your homework assignment).

I also recommend using Unity’s Cinemachine package for this kind of camera work. It works via virtual cameras, and you only need to enable and disable these virtual components to make the main camera transition between points, with nice ease in/ease out movement as well.

1 Like