How do you put a sound on water that is always playing across the surface, not just a singular point?
I'm thinking that you could implmenent this by having a separate invisible GameObject in your scene which has an AudioSource attached (with the looped water sound clip assigned).
You'd then move this audiosource around to follow the player, but have it constrained to the shape of your water. The complexity of implementing this would then be directly tied to the complexity of the shape of your water.
For example, the easiest shape would be a circular pond or lake. In this case, you could use something like this pseudo-code to constrain the water audio object:
Vector3 audioPos = playerPos;
audioPos.y = waterPos.y;
// constrain position to circle around water
if ((audioPos - waterPos).magnitude > waterRadius)
{
audioPos = waterPos + (audioPos-waterPos).normalized * waterRadius;
}
Similarly, a square or rectangular shaped area of water would be fairly simple:
Vector3 audioPos = playerPos;
audioPos.y = waterPos.y;
// constrain x and z to 2d rectangle around water
audioPos.x = Mathf.Max( waterPos.x - waterWidth*0.5f , audioPos.x);
audioPos.x = Mathf.Min( waterPos.x + waterWidth*0.5f , audioPos.x);
audioPos.z = Mathf.Max( waterPos.z - waterLength*0.5f , audioPos.z);
audioPos.z = Mathf.Min( waterPos.z + waterLength*0.5f , audioPos.z;
If your water is more of an arbitrary shape, you'll need a more complex algorithm to determine how to constrain your audio object to the 2d surface of that shape. Perhaps some code which first finds the triangle in your water mesh which is nearest the player, and then some code which constrains the audio object to that triangle.
If your water is made using a large plane which intersects a terrain which dips below the water level in certain areas (as is a common technique), you'll need another method again, because this set-up doesn't readily reveal any data that you can use to determine the nearest area of water. One method that springs to mind to implement this would be to do a quick "scan" of your whole terrain using raycasts, to build up a low-res 2d grid of data representing "water" and "non-water" areas. Then, at runtime, you could have an algorithm which picks the nearest grid cell marked as water to determine the correct location for your audiosource. You'd also need to add some kind of blending between the positions to avoid the audiosource suddenly jumping from one cell position to another.
Anyway... food for thought, it's definitely an interesting question. Good luck!
I need this too and i think the most practical way would be to have a sound object follow the player around and stop at the edge of the water. idk how to do this, though. )= maybe a collider?
I'm going to answer it.
Make the aer a trigger, attach a sound to the player and make a script so when the player enters the trigger, the sound on the player activates.