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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help properly playing ocean waves sound effects.

Discussion in 'Scripting' started by KennethGames2020, Mar 20, 2020.

  1. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Probably the most straightforward way of doing this would be to have a "surf volume controller" script.

    You would sprinkle instances of this script along the edge of your water, and then have them all access the single AudioSource that is making the surf sounds, probably through another script on there that aggregates all the different surf volume controller inputs.

    The surf volume script would monitor distance from player and then report to the "find closest" script, and the find closest would periodically move the volume level of the surf noise up and down based on what it hears from the surf volume script instances.

    ALTERNATELY, you could just have the player raycast in 8 or 12 directions radially looking to hit water, and if they hit water take the closest patch of water and drive the noise from that distance.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can I assume the player will only be on land, so it is really distance to the shore not distance to the water?

    If so I'd place empty GameObjects along the shore and put them all in a list. Then every 5 seconds I'd loop through the list getting distance from all the objects to the player (or if a lot of them, put them in a coroutine spreading the workload across multiple frames). The shortest distance is the player's distance to the shore. Then I'd calculate a new sound volume off that distance depending on what you're looking for, and over the next couple seconds I'd lerp between the previous volume and the new volume so there isn't a jarring volume change.

    This is just off the top of my head.

    If there is a good amount of your game away from the shore, I'd create a trigger collider covering the shore area which you'd use to activate/deactivate the entire water sound system.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Mmm, very wise Uncle Joe is!
     
    Joe-Censored likes this.
  5. hidingso

    hidingso

    Joined:
    Sep 8, 2020
    Posts:
    1
    I have a rather lightweight solution for anyone finding this thread with this question in mind.

    My game has a lot of shorelines, and I mean a lot. While the above solutions work really well, I didn't want to litter my shorelines with prefabs and constantly poll which one is closest to my player.

    My solution: Fill your shoreline with BoxColliders that represent shore areas. Then, using Physics.OverlapBox you can detect if your player has entered a said area. If the player is inside this area, you can just raycast straight down and calculate the "proximity" to water. Unless you have really bumpy landscape and incorrectly placed shore areas, this will give you good results.

    Sure, you still have to place box colliders along the shoreline, but your colliders can be way bigger and you don't have to calculate multiple distances.
     
    Kurt-Dekker likes this.