- Home /
How to set up sound radius in 2D game?
I'm trying to set up a sound effect that can only be heard when the player is in a certain vicinity of an object. Here's what I've tried so far:
-Changed the Spatial Blend from 2D to 3D. -Changed the volume rolloff to Linear. -Set a min and max distance.
When I did this I was unable to hear the sound when entering the radius. Is there another way to do this or am I just doing this wrong? Thank you!
Answer by pyramidhead01 · Jul 17, 2019 at 02:29 AM
You don't need to actually detect the sound, have the object that makes the noise have a script that creates an OverlapCircleAll (or something similar) and then check to see what it hits:
float length = howeverLongYouWantThisToBe;
Collider2D[] soundRadius = Physics2D.OverlapCircleAll(transform.position, length);
foreach (Collider2D obj in soundRadius)
{
if (obj.tag == "WhateverYouAreLookingFor")
{
DoWhateverCodeYouWerePlanning;
}
}
You can better customize this further, but this should be a start of what you are actually trying to do, let me know if you need more help.
I guess I'm a little confused on how to implement this. Would I trigger the sound to play if the tag == my player? I've never used OverlapCircleAll before to be honest.