Hey fellow game programers/designers! I have a problem, that I can’t even begin to think of how to solve.
The Problem: This function is set up as a delegate event, but the problem I’m having is when I send the noise level of the walking, and then the volume of a gunshot, because the walking is continuous and the gunshot isn’t the value of total noise doesn’t go up.
I have the Combat Script sending it’s weapon noise as a value when it is fired, PlayerControl script sending the walking noise when walking is true. In the Universal Player Detection Management Script, the noise level is managed, and always lerped back to 0. The total noise value is sent out as an argument through a delegate event function, which all enemies are subscribed to.
Function Explaination: Basically what the function is, is a function that allows the enemy to hear the player making noise. When it hears noise, it goes and investigates the area the sound came from. It takes into account: the distance of the player and if it’s within the auditory radius, the volume of the noises made (set by me) as variables which affects the distance within the auditory range of hearing that it can be heard (eg. footsteps have to be closer than a gunshot), and then depending on the distance the accuracy of the area it searches (eg. the closer the sound is heard, the closer the point it searches will be to where the player is).
Code:
PlayerControl
//this isn’t the actual code but this is how it is sent
if (walking == true)
{ playerDetection.noiseLevel = walkingNoise; }
PlayerCombat
void Shoot()
{ //Shooting stuff
playerDetection.noiseLevel = gunNoise;
}
//PlayerDetection Script
if (playerNoiseLevel > minNoise)
{
if (PlayerNoise != null)
{
//This is the event call
PlayerNoise(playerNoiseLevel);
}
}
//EnemyAI Detection
void Start()
{
//Event Subscription
PlayerDetection.PlayerNoise += HearPlayer;
}
public void HearPlayer(float noiseLevel)
{
floatplayerDistance = Vector3.Distance (transform.position, Player.transform.position);
//Arewecloseenoughtohear?
if (playerDistance <= hearingRadius)
{
//Getthedistancethenoisebeingmadecanhearat
floatauditoryDist = (noiseLevel / playerDetection.maxNoise) * hearingRadius;
floataccuracy = (1 / investigationAccuracy) * hearingRadius;
Debug.Log ("Auditory Distance: " + auditoryDist + " Distance To Player: " + playerDistance);
//Arewecloseenoughtohearit?
if (playerDistance <= auditoryDist)
{
investigating = true;
if (investigateArea != playerDetection.nullPosition && timer >= investigateResetMin)
{
investigateArea = Player.transform.position;
agent.SetDestination (newVector3(investigateArea.x + Random.Range (-accuracy, accuracy),
0, investigateArea.z + Random.Range (-accuracy, accuracy)));
timer = 0;
}
}
}
}