- Home /
It is possible to have a random audio pitch with one AudioSource and PlayOneShot?
This sample only work when play one audio, when I call it again, both clips play with new pitch.
function Play(audioClip: AudioClip) {
audio.pitch = Random.Range(0.5, 1.5);
audio.PlayOneShot(clip, Random.Range(0.8, 1.2));
}
My solution is to use a pool of audiosource and controlling by self what source is being used, because PlayOneShot don't update AudioSource.isPlaying).
I think a little wierd, but it is the way?
Answer by Sisso · Aug 28, 2013 at 12:57 PM
Because I have no answer I will consider as "No, your best solution is to create a pool of sources."
Answer by M_R_M · Mar 29, 2019 at 05:02 AM
Anyone who is looking for a solution to this just like I was, ignore the snark and venom that Calum and komodor couldn't seem to help themselves with, AlpayKasal's answer worked perfectly for me with playoneshot, and can be very easily applied to JS as well.
These older threads often come up first in google searches, so I'm grateful people respond with solutions, even if they are on old threads. If the question persists and comes up first when looking for a solution, by all means, LET'S RESOLVE IT.
This pool of resources is what makes Unity unique, so Calum and komodor's responses are as inappropriate as they are wrong and detrimental to the very idea of Unity and the reason we're all using it.
Community guidelines state that this should be a comment, by the way. The reasoning behind not resurrecting old threads is simply that the users are generally not active, and the premise of the question can be out of date to the version of the game engine used at the time of the question.
And yet it's still toward the top in the Google search result for "unity play sound at random pitches" (#2 for me).
I think this is what he was saying: Even if the information herein is obsolete/deprecated/whatever, it still comes up and is therefore relevant, if only to pass folks along to the real/needed/better answers.
Answer by AlpayKasal · Jul 04, 2016 at 01:32 PM
@sisso this is an old thread but I thought I'd answer anyway, for future googlers:
put an f character after your floats in the random ranges, that's the first problem I see... here's some code that s working for me (I needed pitch variations).
public class plinkotrigger : MonoBehaviour {
private AudioSource audioSource;
public AudioClip Snare;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "bullets(Clone)")
{
audioSource = GetComponent<AudioSource>();
audioSource.clip = Snare;
audioSource.pitch = (Random.Range(0.6f, .9f));
audioSource.Play();
}
}
}
@Alpay$$anonymous$$asal This is a very old thread that didn't need resurrecting. Also, he was using javascript, and such does not need to add the f character behind his float variables. Please, pay a bit of attention before responding to such an old thread!
it's nonsense, he's asking for PlayOneShot and you respond with Play, it's different method dude and it does different thing :D
not mentioning the F after float ROFL :D