Jump to content

Playing sound on AudioSource correctly (solved)


blizzy78

Recommended Posts

Hi, I wonder what the correct way to play an AudioClip on an AudioSource is. Currently, I attach the AudioSource to the root part of the vessel, then have it play an AudioClip. There are two problems with that, though: 1) It bugs out in map view, the same as engine sounds currently do. 2) It can't be done if there is no vessel, like in the planetarium, or if the vessel is destroyed. Other than that, it works just fine.

What would be the correct way to do it? Thanks!

Link to comment
Share on other sites

I think you can fix issue 1) by using :

youraudioname.audio.panLevel = 0;

It should fix the sound issue when zooming in/out by making your sound "2D" instead of "3D".

Or have a look at Iannic-ann-od's excellent Chatterer Mod, he use MonoBehaviour instead of Partmodule, and it works like a charm.

link : http://forum.kerbalspaceprogram.com/threads/25367-0-21-Chatterer-v-0-5

Hope it helps.

Link to comment
Share on other sites

Oh well.

I don't think it would fix your issue then, sorry.

I am still trying to figure out Iannic-ann-od's method though, because in my Launch countdown mod my code is interrupted as soon as I activate the launchclamp.

So i think I will have to go Monobehaviour way as well.

Link to comment
Share on other sites

The trick is to do just this:


class MyScript : MonoBehaviour {
public void Start() {
// attach the audio source to the script's GameObject
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = ... // set random clip to prevent NullReferenceException when using audioSource.isPlaying or Play()
}
}

Then you can use the AudioSource like normal.

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...