Search Unity

Multiple Cameras and Audio Listeners

Discussion in 'Editor & General Support' started by GonzoCubFan, Dec 28, 2011.

  1. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    I have a scene with 5 cameras that can be switched between. In the Script that is attached to the MainCamera there is an array of all of the cameras as shown below:
    Code (csharp):
    1.  
    2.     public Camera[] allCams;
    3.  
    I also have a function to set the active Camera that I want to use:
    Code (csharp):
    1.  
    2.     public void SetCamera( int camNum )
    3.     {
    4.         foreach( Camera cam in allCams )         // disable all of the cameras and listeners
    5.         {
    6.             cam.enabled = false;
    7.             cam.GetComponent<AudioListener>().enabled = false;
    8.         }
    9.         allCams[ camNum ].enabled = true;       // enable the selected camera
    10.         allCams[ camNum ].GetComponent<AudioListener>().enabled = true;
    11.         renderCam = camNum;                         //  save the current camera index
    12.     }
    13.  
    The method works fine for switching cameras, but for some reason it doesn't seem to disable the audio listeners. Despite calling this method from the Start() method of the script attached to the scene's MainCamera, and even going gonzo on it and putting calls to the function into the Update() method, I still repeatedly get the following message"
    How do I fix this? Any help would be greatly appreciated.

    Thanks in advance,

    = Ed =
     
  2. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    Instead of disabling the listener, remove it completely, then re-add it to whichever one you're switching to.

    The Unity folks should seriously make listeners more savvy. Splitscreen just doesn't sound right without an insanely complex, scripted solution for multiple cameras.
     
  3. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    Thanks Tuah. I tried to implement this, and it seems simple enough to destroy the AudioListener component on each of the cameras. However, even though I thought that a Camera was a GameObject, my attempts to invoke. cameraObject.AddComponent() fail as the function is unrecognized for the camera (where cameraObject is defined as "public Camera cameraObject;"

    What am I doing wrong? How do I add the component to a know Camera object?

    Thanks in advance,

    = Ed =
     
  4. arcane.artist

    arcane.artist

    Joined:
    Dec 28, 2011
    Posts:
    3
    I've just been working on this myself. The easiest way is to create a GameObject that is always in the scene with the one and only AudioListener on it. Then when switching cameras, set the parent of that object to the camera. All you have to do then is align the transform to that camera.

    So something like this.

    Code (csharp):
    1.  
    2. public Transform _AudioListener;
    3. public void SetCamera( int camNum )
    4. {
    5.     allCams[renderCam].enabled = false;
    6.     allCams[ camNum ].enabled = true;       // enable the selected camera
    7.  
    8.     // attach audio listner to camera
    9.    _AudioListener.parent = allCams[ camNum ].transform;
    10.  
    11.     // align audio listener to camera (do after parent)
    12.     _AudioListener.localPosition = Vector3.zero;
    13.     _AudioListener.localRotation = Quaternion.identity;
    14.  
    15.     renderCam = camNum;                         //  save the current camera index
    16. }
    And TADA! One listener, multiple cameras. Hope this helps.
     
    Last edited: Dec 30, 2011
  5. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    Thanks arcane. That seems to have done the trick!

    = Ed =
     
  6. roman_arch

    roman_arch

    Joined:
    Jan 29, 2014
    Posts:
    2
    hi arcane..thanks for the script. first of all i'm a beginner in unity as well as scripting. like you said, i've assigned that on an empty game object and it's showing an error:
    error CS0103: The name "allCams" does not exist in the current context

    need help please...thanks in advance