I have a question that I am hoping one of you guys might be able to answer.
I am working through a tutorial using the stock “Standard Assets” specifically the Vehicle option and the car prefabs. So I have a car moving using the scripts provided with no modification. Now what I would like to do is have two camera’s working, one outside the car following the car smoothly, the other in-car (if the model will allow it or on the front) with all the bumps and crashes and camera-shaking involved :), and be able to switch between them.
At the moment I have one camera that is smooth outside the car attached to another object with the car hierarchy with a script attached, and another with all the bumps, so, to switch between them I need to go to the Inspector and turn one camera on and switch the other off. So, what I would like to know is there is there any way using a script that I can disable one of the camera’s and enable the one that is off and vice-versa using a script? For instance say if I buttonDown “c” go the car view turn on Car Camera and switch off the outside camera, or, button down “s” go to smooth view above and behind the car and turn off car view?
Yeah, if you have both the cameras referenced in a script you can just set enabled on the component and Unity will switch to the most recently ‘enabled’ camera. It’s usually good to disable the other cameras to be sure.
using UnityEngine;
public class CameraSwitcher : MonoBehaviour {
public Camera[] cameras;
int c = 0;
new Camera camera;
void Start () {
camera = cameras[c];
for (int i = 0; i < cameras.Length; i++)
{
cameras[i].enabled = false;
}
camera.enabled = true;
}
void Update () {
if (Input.GetKeyDown(KeyCode.C))
{
camera.enabled = false;
camera = cameras[c];
camera.enabled = true;
c = (c + 1) % cameras.Length;
}
}
}
A curious thing happened when I used your code and that was listed in the console below:
There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
Is this something I need to worry about? Or, should I just ignore it? I just had to make one change to the code so I didn’t need to press the “C” twice to get the camera’s to start switching would that have caused the error? I moved Line 25 to line 22?
You can just ignore it, but it’s not too hard to fix. Each camera object also has an Audio Listener component on it by default. You can either delete the component from all but one camera, or you can enable and disable the audio components in the same way the camera components are being toggled. Get the audio component using camera.gameObject.GetComponent();
Your change sounds fine, it makes more sense that way.
Thankyou for explaining it! I did the latter using your script as a guide for the “enabled” parts and it worked perfectly . So many thanks again for sending me that script, it saved me many hours of headaches. I do like the idea of deleting the component from all the others camera’s though - it’s like, listen up Unity I’m in charge of the sound and where it’s coming from :p.