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.
// 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<PhysicMaterial, MaterialImpact> 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_.physicMaterial,materials*);*_
}
}
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);
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.