Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Possible to change camera using scripting

Discussion in 'Scripting' started by potatoofmemphis, Nov 19, 2016.

  1. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    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
     
    Last edited: Nov 19, 2016
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
    TaleOf4Gamers likes this.
  3. nostalgicbear

    nostalgicbear

    Joined:
    Mar 21, 2013
    Posts:
    98
    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.

    Code (CSharp):
    1. public Camera cam1, cam2; //Two different cameras
    2.     // Use this for initialization
    3.     void Start () {
    4.         cam1.enabled = true;
    5.         cam2.enabled = false;
    6.     }
    7.  
    8.    
    9.     //When you collide with an object called "Wall", turn on cam2, and turn off cam1
    10.     void OnCollisionEnter(Collision other)
    11.     {
    12.         if (other.gameObject.name == "Wall")
    13.         {
    14.             cam2.enabled = true;
    15.             cam1.enabled = false;
    16.         }
    17.      
    18.     }
     
    FermentedCorn likes this.
  4. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    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.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class idk : MonoBehaviour {
    5.     public Camera camera1;
    6.     public Camera camera2;
    7.  
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (gameObject.CompareTag("Pickup"))
    12.             camera1.enabled = true;      
    13.             camera2.enabled = false;
    14.     }
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.    
    24.     }
    25. }
    26.  
     
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Your if statement is not enclosed in curly brackets, and only executes the first line but not the second.
     
    TaleOf4Gamers likes this.
  6. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    oh.. Lol that would explain it

    where would i put the {}
     
  7. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    look at how @nostalgicbear did it.
     
  8. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
  9. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    oh thanks a lot
     
  10. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class idk : MonoBehaviour
    4. {
    5.     // could also think of having an array of Cameras and switch to a previous or next one etc.
    6.     // just an idea
    7.     public Camera camera1;
    8.     public Camera camera2;
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (gameObject.CompareTag("Pickup"))
    12.         {
    13.             EnableCameraOne();
    14.             // for my switch-case example, youd use:
    15.             // ChooseNewCamera(1);
    16.         }      
    17.     }
    18.     void EnableCameraOne()
    19.     {
    20.         camera1.enabled = true;    
    21.         camera2.enabled = false;
    22.     }
    23.  
    24.     // dont know if you need this but I made it anyway
    25.     void EnableCameraTwo()
    26.     {
    27.         camera1.enabled = false;    
    28.         camera2.enabled = true;
    29.     }
    30.  
    31.     // just an idea
    32.     void ChooseNewCamera(int cameraToSwitch)
    33.     {
    34.         switch(cameraToSwitch)
    35.         {
    36.             case 1:
    37.                 EnableCameraOne();
    38.                 break;
    39.  
    40.             case 2:
    41.                 EnableCameraTwo();
    42.                 break;
    43.  
    44.             default:
    45.                 Debug.LogError("Cannot switch to that, camera " + cameraToSwitch);
    46.         }
    47.     }
    48. }
     
  11. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class idk : MonoBehaviour {
    5.     public Camera camera1;
    6.     public Camera camera2;
    7.  
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.  
    12.         if (gameObject.CompareTag("Pickup"))
    13.         {
    14.             camera2.enabled = true;
    15.             camera1.enabled = false;
    16.         }
    17.     }
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.    
    27.     }
    28. }
    29.  
     
  12. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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.)
    Code (CSharp):
    1. Debug.Log("Triggered!");
    2.  
    3. if (gameObject.CompareTag("Pickup"))
    4. {
    5.     camera2.enabled = true;
    6.     camera1.enabled = false;
    7.     Debug.Log("Camera 1 status: " + camera1.enabled);
    8.     Debug.Log("Camera 2 status: " + camera2.enabled);
    9. }
    10. else
    11. {
    12.     Debug.Log("Collision tag is not Pickup, it is " + other.tag);
    13. }
     
  13. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Did you change the tag of both cameras to "Main Camera"?
     
  14. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    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.
     
  15. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    This issue has been asked about before:
    http://answers.unity3d.com/questions/16146/changing-between-cameras.html

    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.
     
  16. potatoofmemphis

    potatoofmemphis

    Joined:
    Oct 8, 2016
    Posts:
    33
    While I have looked at them none of them have answered my problem. I did google everything beforehand and couldnt find anything..