There's more...

We can develop a more flexible algorithm by defining different types of walls that affect sound intensity. It works by casting rays and adding up their values to the sound attenuation:

  1. Create a dictionary for storing wall types as strings (using tags) and their corresponding attenuation:
public Dictionary<string, float> wallTypes; 
  1. Reduce sound intensity this way:
intensity -= GetWallAttenuation(emitterPos, srPos); 
  1. Define the function that was called in the previous step:
public float GetWallAttenuation(Vector3 emitterPos, Vector3 receiverPos) 
{ 
    // next steps here 
} 
  1. Compute the necessary values for ray casting:
float attenuation = 0f; Vector3 direction = receiverPos - emitterPos; float distance = direction.magnitude; ...

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.