0
\$\begingroup\$

I have a character with a bunch of different attacks + sound effects in Unity.

One way to architect this is to create a single AudioSource, and then set the AudioSource.clip to a specific sfx each time an attack happens. E.g.:

playerAudio.clip = laserSfx;
playerAudio.PlayOneShot();

(Where playerAudio is an AudioSource, and laserSfx is an AudioClip loaded at runtime)

Another method is to have child game objects with their own AudioSources, so when I do attack A, I can do something like AttackA.GetComponent<AudioSource>().Play()

Is there a preferred way of doing this in Unity? Considering performance impacts and such.

\$\endgroup\$
1
  • \$\begingroup\$ The way I set it up is having an audio source prefab (that includes all the necessary audio setup). Then add that prefab as a nested prefab to each entity prefab that needs sound. Then the parent prefab uses this nested prefab and passes to it the sound clips it needs to be played. Basically very close to using PlayOneShot (plus some custom setup and optional randomization of the sound configuration). \$\endgroup\$
    – Nikaas
    Commented Dec 29, 2021 at 13:29

1 Answer 1

0
\$\begingroup\$

The question is more about which sound effects can play at the same time and which should be cut hard.

If they can play at the same time, I would consider a Pool, which makes sure that the audiosources gets disabled when returned to the pool. The pooling therefore guarantees there are never too many audiosources in the scene than needed. The pool could even grow when needed or in fact limit how many can be active at once.

\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .