Question by
joerce · Mar 07, 2016 at 03:46 PM ·
triggeraudiosourcekeykeypress
How to use a key as trigger?
I have added the Z key as trigger for an Audio Source (Zombiemusic) connected to a Third Person Controller. It works perfectly, and will start at key press, but when I start the game mode the Audio Sorce is already active and will not stop unless I press a key for another camera view (orbit cameras and a first person controller). How do I ensure that the audio source isn't playing at the start? This is my script:
public class CameraSwitcher : MonoBehaviour {
public GameObject camera1;
public GameObject camera2;
public GameObject camera3;
public GameObject camera4;
public GameObject fpsController;
public GameObject thrdController;
public GameObject Zombiemusic;
void Update() {
if (Input.GetKeyDown ("1")) {
camera1.SetActive(true);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(false);
fpsController.SetActive(false);
thrdController.SetActive(false);
Zombiemusic.SetActive(false);
}
if (Input.GetKeyDown ("2")) {
camera1.SetActive(false);
camera2.SetActive(true);
camera3.SetActive(false);
camera4.SetActive(false);
fpsController.SetActive(false);
thrdController.SetActive(false);
Zombiemusic.SetActive(false);
}
if (Input.GetKeyDown ("3")) {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(true);
camera4.SetActive(false);
fpsController.SetActive(false);
thrdController.SetActive(false);
Zombiemusic.SetActive(false);
}
if (Input.GetKeyDown ("4")) {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(true);
fpsController.SetActive(false);
thrdController.SetActive(false);
Zombiemusic.SetActive(false);
}
if (Input.GetKeyDown ("e")) {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(false);
fpsController.SetActive(true);
thrdController.SetActive(false);
Zombiemusic.SetActive(false);
}
if (Input.GetKeyDown ("z")) {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(false);
fpsController.SetActive(false);
thrdController.SetActive(true);
Zombiemusic.SetActive(true);
}
}
}
Comment
Answer by thedrb · Mar 08, 2016 at 01:13 AM
Also, as a property in the AudioSource component, there is a checkbox for 'Play on Awake'. I had a similar issue, and unchecking that worked for me. :)
Answer by Magius96 · Mar 07, 2016 at 04:20 PM
Add this
void Start()
{
Zombiemusic.SetActive(false);
}
That should work for ya.