- Home /
Script to play different audio clips based on tags
Hi, I'm trying to write a script to play different footstep sounds based on the tag of a surface. So for example if the surface is tagged metal, it would play the metal footstep sounds, and if the surface is tagged floor, it would play the floor footstep sounds.
To sync the footsteps up to the animation, I am using animation events, which is where things get tricky for me. Anyway, I've gleened information from a few other scripts to compile my own that would work with the way my character is set up.
It's not working though, and I also get an error: expecting (, found 'WalkOnMetal'. Here's my script:
var metal : AudioClip[];
var floor : AudioClip[];
function OnControllerColliderHit (hit : ControllerColliderHit) {
var Player : GameObject;
Player = GameObject.Find("Player");
if (hit.gameObject.tag == "Metal") {
WalkOnMetal();
} else if (hit.gameObject.tag == "Floor") {
WalkOnFloor();
}
function WalkOnMetal() {
audio.clip = metal;
audio.volume = .5;
audio.Play();
}
function WalkOnFloor() {
audio.clip = floor;
audio.volume = .5;
audio.Play();
}
Can anyone tell me what I've done wrong?
NOTE:
I had the animation event footstep approach working before with this super simple script:
function PlayAudio (aud : AudioClip){
audio.clip = aud;
audio.Play();
}
It only played one sound though, no matter what the surface.
O$$anonymous$$, forgot to mention one thing. I am not using a Character Controller component on my character because I have a custom script. I was unable to find another function for 'ColliderHit' though, could this be a problem? I see that I have called 'Player' but am not doing anything with it.
You're missing a closing ) at the end of your OnControllerColliderHit. If you aren't using the CharacterController, you probably need to switch to on OnTriggerEnter(after marking the colliders as IsTrigger). $$anonymous$$ake sure you meet the requirements from the doc on OnTriggerEnter
function OnCollisionEnter(hit:Collision)...etc, works the same without needing to use triggers
OH sorry, it was supposed to be AudioSource.blablabla.
var metal : AudioClip;
var floor : AudioClip;
function Start(){
var Player : GameObject;
Player = GameObject.Find("Player");
}
function OnCollisionEnter (hit : Collision) {
if (hit.gameObject.tag == "floor") {
AudioSource.PlayClipAtPoint(floor,transform.position);
}
if (hit.gameObject.tag == "metal") {
AudioSource.PlayClipAtPoint(metal,transform.position);
}
}
I've fixed the few syntax errors.
Answer by Hyperion · Aug 31, 2013 at 10:23 PM
Okay then, here's my answer.
var metal : AudioClip;
var floor : AudioClip;
function Start(){
var Player : GameObject;
Player = GameObject.Find("Player");
}
function OnCollisionEnter (hit : Collision) {
if (hit.gameObject.tag == "floor") {
AudioSource.PlayClipAtPoint(floor,transform.position);
}
if (hit.gameObject.tag == "metal") {
AudioSource.PlayClipAtPoint(metal,transform.position);
}
}
If you have any very specific questions, I might be able to help more.
Thanks again for your help. I just need to figure out how use this with the animation events.
You're welcome. If you have issues finding the answer, you might want to post a separate question to increase chances of getting a good answer.
Answer by citizen_rafiq · Aug 31, 2013 at 08:09 PM
// Assign type of physicsMat in surface then get sound, depending what type of surface in now player standing. you can write new function in class MaterialImpactManager for meet you requirement. public class MaterialImpactManager : MonoBehaviour {
public MaterialImpact[] materials; private static System.Collections.Generic.Dictionary dict; private static MaterialImpact defaultMat;
public void Start() {
defaultMat = materials[0];
dict = new System.Collections.Generic.Dictionary<PhysicMaterial, MaterialImpact>();
for (int i = 0; i < materials.Length;i++ )
{
dict.Add(materials[i].physicMaterial,materials[i]);
}
}
public static AudioClip GetBulletHitSound(PhysicMaterial mat){
MaterialImpact imp = GetMaterialImpact(mat);
return GetRandomSoundFromArray(imp.bulletHitSounds);
}
public static MaterialImpact GetMaterialImpact(PhysicMaterial mat) {
if (mat && dict.ContainsKey(mat)) {
return dict[mat];
}
return defaultMat;
}
public static AudioClip GetRandomSoundFromArray(AudioClip[] audioClipArray) {
if (audioClipArray.Length > 0) {
return audioClipArray[Random.Range(0,audioClipArray.Length)];
}
return null;
}
}
[System.Serializable] public class MaterialImpact {
public PhysicMaterial physicMaterial;
public AudioClip[] playerSounds;
public AudioClip[] bulletHitSounds;
}
// from which class you want to get different sound
sound = MaterialImpactManager.GetBulletHitSound(hitInfo.collider.sharedMaterial);
I feel like this is great information, just need to figure out how to apply it all.
So I'm not sure how to use this, I just get tons of errors when I apply the whole code, or parts of it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Destroy only selected object 1 Answer
Synch clips with BPM 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Ignore collision on tag not working 2 Answers