Play 3D sound for destoryed projectiles


Through out the development process of the week, there were not much thing that is causing trouble. The only thing that is urgent to solve is that whenever a projectile is been destroyed, the audio source component attached to it will also be destroyed with it. That leads to the problem that the hitting sound will not be playerd when projectiles hit something. 

After few tests, I found two potential solutions. The first one is puting an audio source inside the scene, and whenever projectiles hitting something, I can use that audio source, and call PlayOneShot() to play the hitting sound. In this case it dosen't matter if the projectile is been destroyed or not, the sound will always play. The downside of this soulution is that making a 3d hitting sound is impossible because there is only one audio source. 

To make it a 3d sound effect, I used the second solution. This solution is making the 3d sound effect possible. So what I did is whenever projectiles are hitting something, I create a new game object, and then add component of audio source on it, and using a function to copy the desired audio source setting onto this new one, and then I can simply call PlayOneShot from this new audio source, and finally call destroy after few seconds. However, there is also disadvantage for implementing the method,  constantly creating and destroying game objects has a potential performance impact when there are too many audio sources that are using this method. So in the future pooling system will be implemented for things like this.

Author: Zeyuan Song

Posted on 03/28/2024

Get Suits of the Abyss

Leave a comment

Log in with itch.io to leave a comment.