I don’t know how long I’ve been chasing this. In the very beginning it worked fine with just one audio clip and simple code like this >>
aS = GetComponent<AudioSource>(); // This is in Start() of course
// then I had a trigger in Update()
aS.volume = Random.Range(0.8f, 1);
aS.pitch = Random.Range(0.8f, 1.1f);
as.Play();
and it played very nicely. But, I went to add another clip and it quit being so friendly. Next when I asked it to play it constantly restarted. I got around that with the first if(!aS.isPlaying).
Now, I’ve got one clip working and one trying to constantly restart. Let me show you what I’m working with here.
// Update()
moveSpeed = rb.velocity.magnitude;
// FixedUpdate
// Sounds for Stepping
{
if (moveSpeed > 2.1f && moveSpeed < 5.9f)
{
StepSounds();
}
// Sounds for Sprinting
if (moveSpeed > 6.1f)
{
StopSounds(); // I had to put this here or it wouldn't switch!!
SprintSounds();
}
// Sounds for Stopped
if (moveSpeed < 1.9f)
{
StopSounds();
}
}
// and then the individual methods follow >>
void StepSounds()
{
if (!aS.isPlaying) // adding this cured the first one from restarting
{
StopSounds();
aS.volume = Random.Range(0.8f, 1);
aS.pitch = Random.Range(0.8f, 1.1f);
aS.clip = walking;
aS.Play();
}
}
void SprintSounds()
{
if (!aS.isPlaying) // but this one just restarts constantly
{
aS.volume = Random.Range(0.8f, 1);
aS.pitch = Random.Range(0.8f, 1.1f);
aS.clip = sprinting;
aS.Play();
Debug.Log("I should have played Sprint Sounds!!");
}
}
void StopSounds()
{
aS.Stop();
}
So what I’ve got now, is that when walking (moveSpeed more than 2 and less than 6) it plays that clip nicely, like its supposed to. However, when I sprint and moveSpeed exceeds 6 clip number two “sprinting” plays but it constantly tries to restart. I had this problem with the first one, but adding that if(!aS.isPlaying) solved the first clip. I have copied and pasted it to the second method but I’m getting nothing.
I also notice that when I have the if(aS.isPlaying) then the lines of code “volume = Random.Range” don’t actually function like they did once before. Which I can live with. Right now, watching the inspector when moveSpeed exceeds 6, the second clip constantly restarts and I can see volume and pitch bouncing around trying to randomize ha ha. If it would just play through I’d be happy.
I have the Audio Source set to loop, and even tried adding in aS.loop = true; but nothing changed.
If anyone has any ideas I’ll be very greatfull to hear them!