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