- Home /
AngryBots MaterialImpactManager Script and FootstepsAudio
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[i].physicMaterial, materials[i]);
}
}
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;
}
}
If you have the AngryBots project and the AngryBots.unity scene file to open, I can tell you exactly what I see, and maybe you can deter$$anonymous$$e if everything is set up the way it's supposed to:
Upon opening the scene, if you go to the Hierarchy->$$anonymous$$isc->Caches, and look in the inspector pane, there appear to be an array of audio files link to specific Physic materials called $$anonymous$$etal, Catwalk, and Enemy.
Then, if you go back to the Hierarchy->Environment(static)->Catwalks->catwalk_A->polySurface486, it has a $$anonymous$$esh Collider component, and under $$anonymous$$aterial, it's selected as Catwalk.
When I try to walk along the catwalk with my character, I'm not hearing the array of player_ fs_catwalk audio files that I'm expecting to hear listed in the $$anonymous$$aterial Impact $$anonymous$$anager script. I feel like something must be wrong with the code?
I don't see any reason why the code shouldn't work. $$anonymous$$aybe you messed something up in your project. Try to put some temporary Debug.Logs in the code to see what got executed. You can also get the audio clip name right before you play it. There are hundreds of ways to debug your project, however that's up to you...
btw, you posted an answer, but it's not really an answer. I've converted it into a comment. Also you posted it 3 times. Your post got stuck in the moderation queue because you don't have enough karma to directly post ( > 15). So just be patient. Every user with 1000+ karma can publish your posts.
I'm also experiencing the same thing with this demo. The footsteps are not matching the material. I checked that all the files and file names are correct and correctly referenced in the scripts to my best ability but I can't figure out why they are not working correctly. I am not a coder but would like to know how to resolve this issue. Will an experienced coder see if they can debug this issue and explain how they did so for future reference? The OP did a good job finding the correct scripts for reference.
Answer by Bunny83 · Jun 07, 2012 at 11:55 PM
Well i never played around with the AngryBots sample, but i guess it should work ;) Are you sure your colliders have a PhysicMaterial set? I don't talk about visual materials on the renderer. It's about the PhysicMaterial of the collider components which define the physical properties of the object.
Also you need to setup the material->sound array with your footstep sounds.