How to do it...

We will create the SoundReceiver class for our agents, and SoundEmitter for things such as alarms:

  1. Create the class for the sound-receiver object:
using UnityEngine; 
using System.Collections; 
 
public class SoundReceiver : MonoBehaviour 
{ 
    public float soundThreshold; 
} 
  1. Define the function for the behavior that is handling the reception of sound:
public virtual void Receive(float intensity, Vector3 position) 
{ 
    // TODO 
    // code your own behaviour here 
} 
  1. Now, let's create the class for the sound-emitter object:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class SoundEmitter : MonoBehaviour { public float soundIntensity; public float soundAttenuation; public GameObject emitterObject; ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.