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

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...