Possible to change camera using scripting

Hello all,

My question is when my object collides with something can it switch the camera from the main camera to a different camera using scripts?

Or any other way? I dont know anything about audio

Just disable one camera and enable the other. Or have them both on but viewing different layers. Or just teleport the main camera to the target location.

Any number of solutions.

1 Like

This is just a tiny piece of code to help you on your way. It should give you a good starting point to create a far better script.

public Camera cam1, cam2; //Two different cameras
    // Use this for initialization
    void Start () {
        cam1.enabled = true;
        cam2.enabled = false;
    }

   
    //When you collide with an object called "Wall", turn on cam2, and turn off cam1
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.name == "Wall")
        {
            cam2.enabled = true;
            cam1.enabled = false;
        }
      
    }
1 Like

Yes I wrote a piece of code like that but when I tried executing it the camera wouldnt switch. Well it would but what would happen was once the camera became not active the display froze and it wouldnt switch to the other camera. This is the code.

using UnityEngine;
using System.Collections;

public class idk : MonoBehaviour {
    public Camera camera1;
    public Camera camera2;
  

    void OnTriggerEnter(Collider other)
    {
        if (gameObject.CompareTag("Pickup"))
            camera1.enabled = true;       
            camera2.enabled = false;
    }

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

Your if statement is not enclosed in curly brackets, and only executes the first line but not the second.

1 Like

oh… Lol that would explain it

where would i put the {}

look at how @nostalgicbear did it.

But my if statement is in {} though

they are in blue and bolded

oh thanks a lot

yes, thats the issue.

Just to add to this though I did an alternative example. Using custom methods to active different cameras and to show that there are many ways to achieve different scenarios.

using UnityEngine;
using System.Collections;
public class idk : MonoBehaviour
{
    // could also think of having an array of Cameras and switch to a previous or next one etc.
    // just an idea
    public Camera camera1;
    public Camera camera2;
    void OnTriggerEnter(Collider other)
    {
        if (gameObject.CompareTag("Pickup"))
        {
            EnableCameraOne();
            // for my switch-case example, youd use:
            // ChooseNewCamera(1);
        }      
    }
    void EnableCameraOne()
    {
        camera1.enabled = true;     
        camera2.enabled = false;
    }

    // dont know if you need this but I made it anyway
    void EnableCameraTwo()
    {
        camera1.enabled = false;     
        camera2.enabled = true;
    }

    // just an idea
    void ChooseNewCamera(int cameraToSwitch)
    {
        switch(cameraToSwitch)
        {
            case 1:
                EnableCameraOne();
                break;

            case 2:
                EnableCameraTwo();
                break;

            default:
                Debug.LogError("Cannot switch to that, camera " + cameraToSwitch);
        }
    }
}

now

it doesnt work at all. The camera doesnt stop rendering or anyything. It just keeps on using the first camera.

this is my current code:

using UnityEngine;
using System.Collections;

public class idk : MonoBehaviour {
    public Camera camera1;
    public Camera camera2;


    void OnTriggerEnter(Collider other)
    {

        if (gameObject.CompareTag("Pickup"))
        {
            camera2.enabled = true;
            camera1.enabled = false;
        }
    }

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

Are you sure?
I say this all the time but for the love of god watch videos on how to debug.
Try this within your trigger enter.
(I dont know if itll work its hand typed.)

Debug.Log("Triggered!");

if (gameObject.CompareTag("Pickup"))
{
    camera2.enabled = true;
    camera1.enabled = false;
    Debug.Log("Camera 1 status: " + camera1.enabled);
    Debug.Log("Camera 2 status: " + camera2.enabled);
}
else
{
    Debug.Log("Collision tag is not Pickup, it is " + other.tag);
}

Did you change the tag of both cameras to “Main Camera”?

Why would I need to change the tag to Main camera? I have never put a tag on any one of the cameras.

Allow me to clarify I dont think i provided a proper explanation, what happens is when my player object collides with the object that has the pickup tag the camera i want to turn off or stop rendering doesnt (camera2) and the other camera doesnt turn on (camera1). I have no idea why nothing is working now because earlier at least one camera would stop rendering.

This issue has been asked about before:

It’s not so much about me or anyone else not understanding your problem, but you yourself do not seem to have made an effort to explore our answers, or even search it out yourself it seems. I say that because the above link was the first result from a google search using the terms “unity camera switching”. It provided all the information you would have needed.

While I have looked at them none of them have answered my problem. I did google everything beforehand and couldnt find anything…