I’ve started using Unity recently and have ran into some snags with the built audio features. I am trying to have multiple sounds play from the Player Game Object. I’m not sure what the best way to do this.
I have looked into solutions that involve changing the audio clip in the audio source. The problem I ran into with this method was that using the function “PlayOneShot” allows the sounds to stack. So if I try using this method for the walking noise it creates a new instance of that sound that layers on top of each other each frame the player walks. I figure I need to make just a player noise track that starts and stops depending on when the player is walking.
I’ve also considered trying to use multiple audio sources on the game object.
I’m really not sure what the best approach is for this and I’m having a hard to finding resources in this subject area.
I’ve used multiple audio sources and multiple clips in the past to do this, but I too am not sure if that’s the correct approach. What about having the sound of the player’s feet track play on loop, but only turn it up when the player is moving? Same effect right?
if (gameObject.GetComponent<Rigidbody>().velocity.magnitude>0)
{
audioSource.volume = 1;
}
else
audioSource.volume = 0f;
You can use more than one audio source if you deem it necessary. PlayOneShot can work for limited circumstances, but there are problems with using only one source if you need to stop specific sounds, if the object “producing” the sound is large enough that different parts of it exists in significantly separated areas of the scene, or generally when you need to manage looping sounds.
Put another audio source in a child object and use that one for footsteps independent of your other source. You can even position it at the character’s feet if it makes a difference.
Yeah, PlayOneShot isn’t very useful. We never use it actually, because it’s “fire and forget”, meaning you lose the ability to stop/change position and everything else once you tell it to play. Unless you stop ALL the one-shots playing on the Audio Source. Hyblademin’s suggestion is a better way to do it.