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. Dismiss Notice

Camera switching

Discussion in 'Scripting' started by Ruckrova, Nov 26, 2019.

  1. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Im trying to switch between three cameras but its not working as i would like , I just want to go to each camera as a hit a key ( c = main v = tail and b = Warf ) i think this script is just for two cameras .

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class camSwitch : MonoBehaviour
    8. {
    9.     public Camera MainCamera;
    10.     public Camera CameraTail;
    11.     public Camera WarfCamera;
    12.  
    13.     void Awake()
    14.     {
    15.  
    16.         MainCamera = Camera.main;//get a reference to the main camera
    17.                                  //CameraTail = Camera.main;//get a reference to the tail camera
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if ((MainCamera != null) && Input.GetKeyDown(KeyCode.C))
    23.         {
    24.             MainCamera.enabled = MainCamera.enabled ? false : true ;
    25.             CameraTail.enabled = CameraTail.enabled ? true : false;
    26.             WarfCamera.enabled = WarfCamera.enabled ? true : false ;
    27.         }
    28.         if ((WarfCamera != null) && Input.GetKeyDown(KeyCode.V))
    29.         {
    30.             MainCamera.enabled = MainCamera.enabled ? true : false;
    31.             CameraTail.enabled = CameraTail.enabled ? false : true;
    32.             WarfCamera.enabled = WarfCamera.enabled ? true : false;
    33.  
    34.         }
    35.         if ((MainCamera != null) && Input.GetKeyDown(KeyCode.B))
    36.         {
    37.             MainCamera.enabled = MainCamera.enabled ? true : false;
    38.             CameraTail.enabled = CameraTail.enabled ? true : false;
    39.             WarfCamera.enabled = WarfCamera.enabled ? false : true;
    40.         }
    41.  
    42.  
    43.     }
    44. }
    Also I would the sound to switch with the cameras .
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    When you get a key, don't toggle the camera, just set the camera to what you want.

    i.e., on C, set the C camera true and set the others false.

    Lather rinse repeat for V and B
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You say it doesn't work as you'd like, but you never mention what it is doing you don't like. I suspect you have the wrong enabled/disabled states for one or more of the cameras when you first use this script, or you have the wrong camera tagged as MainCamera.
     
  4. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Thanks for the replies .

    I just want to go to each camera when pressing keys .

    Kurt can you please write the line of code you are referring to as I tried but could not get that to work .

    MainCamera.enabled = true ;
     
    Last edited: Nov 27, 2019
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This is the line of code I would write.

    As Uncle Joe pointed out, "doesn't work" or "could not get it to work" is not helpful.

    - what do you expect
    - what is happening
    - any errors?
    - what is the full relevant code?
     
    Joe-Censored likes this.
  6. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class camSwitch : MonoBehaviour
    6. {
    7.     public Camera MainCamera;
    8.     public Camera CameraTail;
    9.     public Camera WarfCamera;
    10.  
    11.     void Awake()
    12.     {
    13.  
    14.         MainCamera = Camera.main;//get a reference to the main camera
    15.                                  //CameraTail = Camera.main;//get a reference to the tail camera
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if ((MainCamera != null) && Input.GetKeyDown(KeyCode.C))
    21.         {
    22.             MainCamera.enabled = true;
    23.             CameraTail.enabled = false;
    24.             WarfCamera.enabled = false;
    25.            
    26.         if ((WarfCamera != null) && Input.GetKeyDown(KeyCode.V))
    27.         {
    28.             MainCamera.enabled = false;
    29.             CameraTail.enabled = true;
    30.             WarfCamera.enabled = false;
    31.          
    32.         }
    33.         if ((MainCamera != null) && Input.GetKeyDown(KeyCode.B))
    34.         {
    35.             MainCamera.enabled = false;
    36.             CameraTail.enabled = false;
    37.             WarfCamera.enabled = true;
    38.            
    39.         }
    40.  
    41.  
    42.     }
    43. }
    Thanks again for your help, I have sorted it out .

    Sorry I am not the best at explaining what i want or how its not working but I did get the script to work as I wanted with trial and error . It's just simple camera switcher and the tutorial I followed made it more complicated than I needed .

    What I still can't work out is when I switch to the camera on the ground ( warfcamera ) to watch the plane fly over the the sound is still on the main camera so there is no sound of the plane approaching its the same as if you are still in the plane . Also when i turn all the audio Listeners are off on the cameras I still hear the plane ?