The question is answered, right answer was accepted
OnTriggerEnter Footsteps Multiple Times A Second
Hi all! So I have a problem in that my footstep script will get called multiple times a second when the AI Object is walking around. I have it setup like the following picture: When the Objects foot hits the ground it will signal to play a random sound. At first I can't hear the sound but after a bit of walking around I notice that it is playing the sound multiple times every single second it seems. How do I prevent this? I have looked at the following for help with no luck:
http://answers.unity3d.com/questions/738991/ontriggerenter-being-called-multiple-times-in-succ.html
The following code will play a sound based on the material that you are colliding with:
using UnityEngine;
using System.Collections;
[System.Serializable]
class MaterialList {
public string name;
public int number;
public AudioClip[] soundsToPlay;
}
[RequireComponent(typeof(AudioSource))]
public class FootStepTrigger : MonoBehaviour {
[SerializeField] private MaterialList[] materialList;
[SerializeField] private AudioSource audio;
[SerializeField] private bool useTerrain = false;
[Range(0.0F, 1.0F)]
[SerializeField] public float audioVolumeOnStep = 1.0f;
[SerializeField] private bool debugFootSteps = false;
private bool isColliding = false;
private int surfaceIndex;
private string hitMaterial;
void OnStart()
{
if (audio == null)
{
audio = GetComponent<AudioSource> ();
}
}
void OnTriggerEnter(Collider collision)
{
if (debugFootSteps == true) {
if (collision.gameObject.name == this.gameObject.name) {
Debug.Log ("Detecting Myself");
} else {
Debug.Log (collision.gameObject.name);
}
}
if (useTerrain == true)
{
surfaceIndex = TerrainSurface.GetMainTexture (transform.position);
}
foreach (MaterialList material in materialList)
{
if (collision.gameObject.GetComponent<MeshRenderer> ())
{
hitMaterial = collision.gameObject.GetComponent<MeshRenderer> ().material.name;
if (useTerrain == false && (hitMaterial == material.name || hitMaterial == material.name + " (Instance)"))
{
audio.volume = audioVolumeOnStep;
audio.clip = material.soundsToPlay [UnityEngine.Random.Range (0, material.soundsToPlay.Length)];
audio.Play ();
}
else if (useTerrain == true && surfaceIndex == material.number)
{
audio.volume = audioVolumeOnStep;
audio.clip = material.soundsToPlay [UnityEngine.Random.Range (0, material.soundsToPlay.Length)];
audio.Play ();
}
}
}
}
}
Any Suggestions?
UPDATE
It seems like the OnTriggerExit is getting called at the same time that the OnTriggerEnter is getting called. Strange behavior. Also the OnTriggerStay gets called (27) times then never again. This is very strange indeed.
Answer by Jason2014 · May 19, 2016 at 08:34 AM
Much better solution is to add an event in animation window. See my previous answer on this link: Make animation do damage
While this would actually solve my problem as far as foot sounds go it unfortunately isn't what I am looking for. I would like this script to not be tied to a specific animation. That's why I went with triggers ins$$anonymous$$d. That why it would hopefully play the sound independent of key frames on an animation.
Follow this Question
Related Questions
Soundarray for player is not working properly 1 Answer
Audio Source does not contain definition for any of the Play functions... 2 Answers
Adding a sound to my Gun Script 0 Answers
Audio or Music continuing to play between scenes 1 Answer
How to stop a sound playing once another starts? (C#) 1 Answer