In this blog post I’m gonna talk about the aliens in our game and describe their sight and hearing ability.
Aliens will walk around on planet surfaces using Unity’s NavMesh. If they spot or hear the player they will react – depending on the race – by fleeing of attacking.
The condition if the player can be spotted depends on two elements:
- a SphereCollider which sets the maximum distance for the alien’s sight
- a check, if the player is in the field of view of the alien
This means, the player is only spotted, if he is inside the SphereCollider (checked in OnTriggerStay) and within the field of view of the alien.
The following screenshot illustrates the maximum sight-range with the outer SphereCollider (green lines) and the field of view with (white lines).
The maximum range an alien can hear is defined by the inner SphereCollider (green lines) in the above screenshot.
If the player is within this SphereCollider a path to him based on the NavMesh will be calculated. If the length of that path is smaller than the radius of the inner SphereCollider, the player is heard by the alien.
That means, if there is something between the alien and the player (like a wall e.g.) the sound has to travel around the object and the path gets longer.
We also let audio sources overlap each other – a loud audio source (like a mining station) will drown out a quiet one (like footsteps).
To achieve this we define a “real-world” volume for every audio source, calculate their volume in proportion to the aliens position and comparing them to each other.
To calculate the audio volume based on the distance of the source we use the formula:
L2 = L1 – Abs(20 * log10(r1/r2))
For example: a car from a distance of 10m has a volume of 60dB. To calculate the volume for a distance of 100m the formula is: L2 = 60 – Abs(20 * log10(10/100)) = 40dB
The player is only heard by an alien, if the following conditions are met:
- the player is within the SphereCollider
- the path to the player is smaller than the radius of the SphereCollider
- the “real-world” volume of the player (e.g. his footsteps) is greater then the maximum “real-world” volume of all other audio sources reaching the alien multiplied by * 0.5
The factor 0.5 is used to consider the fact that – even if one audio source is louder than an other – aliens can hear the quieter one as well. That means if it’s not too quiet.
The Stealth Project from the Unity tutorials explains many of the described features in detail and with source code so have a look if you want to dig in deeper.
If you have any questions or suggestions please leave a message in the comment section below of get in touch via Twitter.