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!
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.
@LevitateWind
Old question, but figured I’d post a potential answer.
I want to say @pyramidhead01 response seems similar to just using OnTriggerEnter2D
with a CircleCollider2D
as a trigger.
When OnTriggerEnter2D
is called, do what you need to do (set a boolean, play a specific one shot SFX, etc.).
If it’s a looping SFX/music, then use OnTriggerExit2D
to pause or change the SFX/music when leaving the CircleCollider2D
’s radius.
I’ve never used Physics2D.OverlapCircleAll
either, so this is just an assumption or another alternative to doing the same thing as @pyramidhead01 answer.