I played through the AngryBots scene that comes with Unity and noticed that the footstep sounds do not change in accordance with the type of material the player/character is walking on. I am pretty new to scripting and Unity in general, but it seems like the FootstepHandler script and MaterialImpactManager script are supposed to be responsible for making this happen, but for some reason it’s not working when playing the game.
How would one modify these scripts to make this happen? It looks like the scripts are set up for this, but maybe something is missing? Any help would be appreciated.
Here is the FootstepHandler code:
#pragma strict
enum FootType {
Player,
Mech,
Spider
}
var audioSource : AudioSource;
var footType : FootType;
private var physicMaterial : PhysicMaterial;
function OnCollisionEnter (collisionInfo : Collision) {
physicMaterial = collisionInfo.collider.sharedMaterial;
}
function OnFootstep () {
if (!audioSource.enabled)
{
return;
}
var sound : AudioClip;
switch (footType) {
case FootType.Player:
sound = MaterialImpactManager.GetPlayerFootstepSound (physicMaterial);
break;
case FootType.Mech:
sound = MaterialImpactManager.GetMechFootstepSound (physicMaterial);
break;
case FootType.Spider:
sound = MaterialImpactManager.GetSpiderFootstepSound (physicMaterial);
break;
}
audioSource.pitch = Random.Range (0.98, 1.02);
audioSource.PlayOneShot (sound, Random.Range (0.8, 1.2));
}
----------
And here is the MaterialImpactManager code:
----------
#pragma strict
class MaterialImpact {
var physicMaterial : PhysicMaterial;
var playerFootstepSounds : AudioClip[];
var mechFootstepSounds : AudioClip[];
var spiderFootstepSounds : AudioClip[];
var bulletHitSounds : AudioClip[];
}
class MaterialImpactManager extends MonoBehaviour {
var materials : MaterialImpact[];
private static var dict : System.Collections.Generic.Dictionary.<PhysicMaterial, MaterialImpact>;
private static var defaultMat : MaterialImpact;
function Awake () {
defaultMat = materials[0];
dict = new System.Collections.Generic.Dictionary.<PhysicMaterial, MaterialImpact> ();
for (var i : int = 0; i < materials.Length; i++) {
dict.Add (materials_.physicMaterial, materials*);*_
* }*
* }*
* static function GetPlayerFootstepSound (mat : PhysicMaterial) : AudioClip {*
* var imp : MaterialImpact = GetMaterialImpact (mat);*
* return GetRandomSoundFromArray(imp.playerFootstepSounds);*
* }*
* static function GetMechFootstepSound (mat : PhysicMaterial) : AudioClip {*
* var imp : MaterialImpact = GetMaterialImpact (mat);*
* return GetRandomSoundFromArray(imp.mechFootstepSounds);*
* }*
* static function GetSpiderFootstepSound (mat : PhysicMaterial) : AudioClip {*
* var imp : MaterialImpact = GetMaterialImpact (mat);*
* return GetRandomSoundFromArray(imp.spiderFootstepSounds);*
* }*
* static function GetBulletHitSound (mat : PhysicMaterial) : AudioClip {*
* var imp : MaterialImpact = GetMaterialImpact (mat);*
* return GetRandomSoundFromArray(imp.bulletHitSounds);*
* }*
* static function GetMaterialImpact (mat : PhysicMaterial) : MaterialImpact {*
* if (mat && dict.ContainsKey (mat))*
* return dict[mat];*
* return defaultMat;*
* }*
* static function GetRandomSoundFromArray (audioClipArray : AudioClip[]) : AudioClip {*
* if (audioClipArray.Length > 0)*
* return audioClipArray[Random.Range (0, audioClipArray.Length)];*
* return null;*
* }*
}
----------