Jump to content

A question for modders on playing sounds


Recommended Posts

Hello.

I am woking on a mod that plays some sounds and have several troubles with them. Could you help me a little?

The code is:

GameObject audioPlayer = new GameObject();
AudioClip heartClip;

heartClip = GameDatabase.Instance.GetAudioClip("Sounds/heart-beat");
heartAudio = audioPlayer.AddComponent<AudioSource>();
heartAudio.volume = GameSettings.VOICE_VOLUME * heartBeatsVolume;
heartAudio.panLevel = 0f;
heartAudio.dopplerLevel = 0;
heartAudio.bypassEffects = true;
heartAudio.loop = true;
heartAudio.clip = heartClip;

Nothing special here, just like in any other mod. My one is initialized in Flight mode too. But then I have to neccessarily do the following or else none of my sounds are played in cockpit:


audioPlayer.transform.SetParent(FlightCamera.fetch.mainCamera.transform);

Without it the sound source seems to be bound to KSC (have heard the sound when flew very close to it).

Other mods using sounds don't have such transform assignment and their sounds are played with no problem. What am I doing wrong?

There's another problem: when I load a save game and then switch to the map view and back it seems that KSP plays all the loaded AudioSources at once (you can hear weird stock engine boost sounds sometimes when switching back to flight). And mine are played too and that's weird escpecially for the looped AudioSources and I'm sure they are not triggered by my code. Other mods don't have such problem. What's the reason and how to deal with that?

Would appreciate your help.

Edited by Ser
Link to comment
Share on other sites

Thanks. So what's your problem? Set the gameObject's transform's parent to transform of the object you want to emit your sound.

Interesting thing is that I need to set this too to have sounds "inside" the pilot's head. But when I searched through the Chatterer source code I haven't found any such bindings and somehow it happens that Chatterer works.

And that's not the main trouble, though it is possibly leads to a more annoying issue: having sounds coming from nowhere when switching back from the Map view. Do you have such issue as well?

Edited by Ser
Link to comment
Share on other sites

No. As it turns out it was because I was accidentally restricting the code from adding a audio source to ALL the ports. It was only adding one to the first port. Most bugs turn out like this, totally DUH! once I realize what's up. I suffer from having a "single linear thread" frame of mind, coming from my earliest experience programming in BASIC.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...