Suppose this situation:
-The Blue area is our AudioRoom, which has the shape of the Box.
-We don't where exactly the player will go.
And we want to give the player feeling of hearing the sound fully distributed in this area, by manipulating the position of the 1 AudioSource.
So how to do this?
Well, it is a solution, because we can tell exactly when
the sound will be distributed on 100%.
I said "when" becouse, I can only imagine this woriking like:
If player will touch the trigger Then Set position of AudioSource to Somewhere;
Else If player will touch the AudioRoom Then Set position of AudioSource to Player;
Or we could try to change Audio from 2D type to 3D type when we will know, that player is exactly in the AudioRoom.
The problem starts when we start to wonder about the number of triggers. In this situation, we would have to place at least 2 triggers to change the position of the AudioSource (Blue path).
The amount of triggers depends really on the situation that we have. But Triggers might be hard when we start to wonder about this situation:
Here we can place 1 trigger outside the AudioRoom. However this time we would have to always change the position of the AudioSource to the at Player Y position. In this case, we can simplify this by setting the position Y of the AudioSource to the Player's Y position all the time.
And so as you may simply imagine there can be lots of other combinations that will require this kind of tricks with the AudioSource.
So... Triggers are ok, but there can be too many trigger system' combinations, for 1 AudioRoom.
Closest point from the Player?
One day I saw a video on youtube about raymarching render technic, that was using a Function called "Distance Estimator" for each object. And depending on what the object's shape is, we are going to use the special function for it.
Each Function had to return a distance value. So in case of asking for the shortest distance from point a
to the circle at point b
with radius r
, we would return.
And since the distance from the sphere was easy, I started to wonder about the box, or a Cube(as firstly i started calling it).
At the start, I Had some problems, with the beginning. I didn't know exactly if I should describe a cube-like, from point A to point B, or from Point Center (center of the cube) and size(of the cube).
I knew that calculating a distance from it will be more problematic since there are more combinations of doing it properly
As You can see here, We have 2,3,4 situations, which we have to recognize. Case 1 and 3 are just simple subtraction. Case 4 should always return us 0. But Case 2 is just a Pythagorean theorem, However, in Case 5 we have to add size instead. Case 6 gets a little bit harder. We know that we are going to calculate the distance from the corner however this time we should add size with a negative Y value.
And so when i was thinking about the distance i started to think about point in the box. And I came up with this:
This method bassicly minimize vector3 deltapos to the special box described as size, and later by adding this to the center, we get the closest point
And This give us a big step for all the cases. Later if we would have finnalize our Distance Function we would just have calculate the lenght of the returned Vector3.
And Yeah going back to our challenge. We want to figure out how to create an AudioRoom. And so by using this technic we can just do this by simply changing the position of our AudioSource to the position of this returned from this function.
We have got the solution for one. What about 2 AudioRooms?
We know how to get the closest position from the box. So how about 2 AudioRooms. Let's analyze this situation:
In this Situation, I marked some AudioSources with small and large radius. Here I want to show the other problem.
Suppose that we would like to use AudioSource2(in AudioRoomBox1) and AudioSource5(in AudioRoomBox2).
If we would not try to touch the AudioRooms Position everything would look fine. However, If we would pick AudioSource4(in AudioRoomBox1) and AudioSource5(in AudioRoomBox2), then there is a small chance that we would start to hear 2 AudioSources in 1 area, and that would be problematic.
Of course, we can leave it like this because in most cases it is not so much noticeable. Just Keep in mind this small, but easy problem.
So we have got 2 soultions to use 2 AudioRooms.
1. We can try to use the only AudioSource1 and set its position to AudioRoom that will have the closest distance to the player.
2. We can try to use 2 AudioSources, and this would be very easy because we could try to give each AudioSource his specific AudioRoom.
The only problem with secound option is that 2 AudioSources are more expensive for calculations than just only 1.
So I am going to try to focus on the first solution.
How we would do this? Well, we can try to create a table of AudioRooms, and AudioRoom that will return us the closest distance.
And here is exactly how it works. First of all, I use the string 'key' value to mark each structure which is always stored somewhere in AudioRoom.structures, a static List of AudioRoomStructure, so that it is later for me easy to find a structure and then try to find the closest point from each structure.
What about the other shapes?
Well let me show you this trick:
AudioRoomStructure is holding a refrences to the interfaces, and so by that i gave an ability to create their own shape of an AudioRoom.
Here are other types of shapes:
Vector3 AudioRoomArea.Locate(Vector3 position)
Vector3 AudioRoomArea.Locate(Vector3 target)
Vector3 actualPos = transform.position + pos;
Vector3 delta = actualPos - target;
float distance = Mathf.Sqrt(Mathf.Pow(delta.x, 2) + Mathf.Pow(delta.y, 2) + Mathf.Pow(delta.z, 2));
delta *= Mathf.Min(radius, distance) / ZeroCheck(distance);
return actualPos - delta;
Oh yeah and sorry for my English, this is my first long article.