Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Instanciating audio sources

Discussion in 'Audio & Video' started by fierarux777, May 14, 2022.

  1. fierarux777

    fierarux777

    Joined:
    Feb 11, 2019
    Posts:
    35
    Hello!
    I want to create an explosion SFX for a artillery shell. When the shell hits the ground or whatever, I want to instantiate an audio source which playes the SFX at the location where the shell hit. I get the location with raycast, but I cant figure out how to play the clip from the audio source on the shell SFX prefab(I made it a prefab tobe able to instantiate it separatelly from the artillery shell). Whatever I did, every time I got a warning that I cant play a deactivated audio source. Taking in consideration that every time after the shell hits something, I want to destroy it's instance.
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    137
    You might want to try play clip at point. This plays an audio clip at a specific position by generating an audio source and then deleting it for you. This can be useful for playing a sound that lasts longer than the object that triggered it.

    I wrote an article about it here: https://gamedevbeginner.com/how-to-play-audio-in-unity-with-examples/#clip_at_point

    otherwise, you might want to try an audio source pool and just move them around as you need.

    Hope that helps
     
  3. fierarux777

    fierarux777

    Joined:
    Feb 11, 2019
    Posts:
    35
    Thanks!
    Much apreciated! :)
     
  4. fierarux777

    fierarux777

    Joined:
    Feb 11, 2019
    Posts:
    35

    Oh and I have another minor problem. Even though the temporary audio source appears, I cant hear anything. I checked and the volume was set to 1.