How to mute sounds that are coming from game objects not in camera
So, basically that, i have a bunch of game objects on a 2D game, some of then shoot projectiles at fix intervals and what i want to do is to mute the sounds they make when they are off camera 'cause right now is just a mess of shooting sounds all over the place, so, how do i achieve such a thing, or what would you suggest as a way to solve this problem ?, thanks in advance :D.
you can check if the object transform position is out the screen & then :
disable the game object
or stop playing the audio or pause it
How do i go about doing that ? is there any specific method i can use to tell is the object is "on camera" ? or do i have to manually check on the update method if x object is between the physical boundaries of the "camera box" (so to speak) ? and if so, wouldn't that be extremely inefficient ?
Answer by Q-ro · Feb 23, 2016 at 07:42 AM
So i ended up setting a bool flag called "_isOnCamera" and using the following unity methods to manage it:
void OnBecameInvisible() {
_isOnCamera = false;
}
void OnBecameVisible() {
_isOnCamera = true;
}
then it was just a matter of playing the sound or not depending on the state of the flag, anyway, i wish i could be done using some sort of 3D sound dampening of sort, that is, if the object is far away, you may hear it but it's pretty low, and it gets louder as the payer gets near the source.
Also wondering if this is indeed the best solution or if there are better ways to go about this.