Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Dec 27, 2016 at 07:37 PM by wesleywh for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by wesleywh · May 18, 2016 at 11:47 PM · c#scripting problemaudio

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: alt text 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.

capture.png (328.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wesleywh · May 19, 2016 at 10:35 PM 0
Share

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

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges