Noise check

for stealth, I’d like to create a noise detection system.

for example, on the objects we would have:

var noise = 0

function NoiseMaker()
{
  if (jumping == true) {noise = 10;}
  else if (running == true){noise = 5;}
  else if (walking == true) {noise = 3;}
  else if (crouching == true) {noise = 1;}
}

etc etc.

How would you use a script to detect within a set range, if there is a noise above a certain level, and also what object made the noise, so it can access variables?

There’s also another script I am working on that needs the same style of code.

You could use an Overlap Sphere with your noise level (times some multiplier value) as radius to detect objects that are within range. Once you have the objects, the easiest (though not most efficient) way would be to use SendMessage to send a notification to the objects you found. Could look something like this:

function MakeNoise(p_noiseLevel)
{
    var foundObjetcs:Collider[] = Physics.OverlapSphere(transform.position, p_noiseLevel * someMultiplicator);
    for(var currentObject:Collider in foundObjects)
    {
        currentObject.transform.gameObject.SendMessage("NoiseReceived", this, SendMessageOptions.DontRequireReceiver);
    }
}

and then in your receiver objects have a function

function NoiseReceived(source:GameObject)
{
    //do something
}