Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by grossim · Mar 21, 2013 at 08:02 PM · audiotriggertagsfootsteps

Footsteps sound on collision problem, please help!!!

Hello there, i'm testing some C# scripts in Unity and in this case i'm trying to get a "Ground Detector" wich is a simple box collider parented to each player's foot, able to get tags from game objects (triggered) and play a different sounds depending on it.

I'm using the locomotor system, so with this "detector" i can obtain an effective ground detection, depending on the real foots/ground contact. Every foot get a ground tag independently, and send a command to the main Audio Source, wich play the right sound.

In some cases foots are on different surfaces (for ex: one on grass, one on concrete), and my Audio Source play the corresponding audio file correctly.

So, till this point the script works well, but sounds are a bit confusing because sometimes they're played more than one at time. Also, when running, audio seem delayed.

I think one of the problem is about multiple ground collision on each step, with produce multiple detections, and in the end multiple sound played for each step.

How to solve that?

I'm also concerned about calculations (is this the reason that cause audio delaying?)...is my method optimized?

Here's the script

 using UnityEngine;
 using System.Collections;
 
 public class SoundEffectController : MonoBehaviour {
     
     
     public AudioSource footAudioSource;
     public AudioClip[] cementsteps;
     public AudioClip[] grasssteps;
     public AudioClip[] dirtsteps;
     
     
     void OnTriggerEnter(Collider col){
          if (col.tag == "pietra")
         {
             footAudioSource.PlayOneShot(cementsteps[Random.Range(0, cementsteps.Length)]);
         }
         else if(col.tag == "erba")
         {
             footAudioSource.PlayOneShot(grasssteps[Random.Range(0, grasssteps.Length)]);
         }
         else if(col.tag == "terra")
         {
             footAudioSource.PlayOneShot(dirtsteps[Random.Range(0, dirtsteps.Length)]);
         }
 }
     }

My knowledge is limited for now, and i'm trying to understand scripts all around the web, mixing the useful parts for my purposes, so my tests are full of errors or illogical methods.

I'm new here, on UnityAnswer and in Unity coding and english is not my mothertongue, so be patient with me if you can :D.

I hope I explained it clearly.

Cheers.

Comment
Add comment · Show 2
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 Fattie · Mar 23, 2013 at 03:18 PM 0
Share

don't use "else if". Generally never use it unless you are very very experience, so that's that. Try code like this:

 if ( blah ... )
 {
 do something;
 return;
 }
 if ( blah ... )
 {
 do something;
 return;
 }
 if ( blah ... )
 {
 do something;
 return;
 }

note the "return"

Secondly it is actually very hard to write foot-fall code.

You need a STATEFUL BOOLEAN, like this

var IJusrDidAFootStepLboolean;

when you hit the ground, set it true.

after that, make a timer -- use Invoke() -- to leave it true for say 0.75 seconcds, and only then set it to false

while it is true, don't play another footfall sound

avatar image grossim · Mar 23, 2013 at 03:34 PM 0
Share

Thankyou Fattie, could you be more explicit about the stateful boolean and the timer? I undestand the logical sense of it, but i don't know how to implement it in my code.

Do you know on the web an example of that?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Mar 23, 2013 at 03:38 PM

Your script seems ok, assuming that the triggers are attached to the foot bones. Maybe your problem is caused by the trigger going below the ground - this would cause a second OnTriggerEnter event, firing the spurious footstep. If this is the case, adjusting the trigger size and position may solve the problem - I would try a small sphere childed to each feet.

Besides this, adding a dead time after each footstep could help - like this:

 public AudioSource footAudioSource;
 public AudioClip[] cementsteps;
 public AudioClip[] grasssteps;
 public AudioClip[] dirtsteps;
 public float stepInterval = 0.25f;

 private float nextStep = 0f;
 
 void OnTriggerEnter(Collider col){
   if (Time.time > nextStep){ // no sound in the dead time
     nextStep = Time.time + stepInterval; // set min time for next step
     if (col.tag == "pietra"){
       footAudioSource.PlayOneShot(cementsteps[Random.Range(0, cementsteps.Length)]);
     }
     else if(col.tag == "erba")
     {
       footAudioSource.PlayOneShot(grasssteps[Random.Range(0, grasssteps.Length)]);
     }
     else if(col.tag == "terra")
     {
       footAudioSource.PlayOneShot(dirtsteps[Random.Range(0, dirtsteps.Length)]);
     }
   }
 }
Comment
Add comment · Show 2 · 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 grossim · Mar 23, 2013 at 04:20 PM 0
Share

Hey aldonaletto, i've followed your instructions and it worked!!!

Here's your tips integrated in my code:

 using UnityEngine;
 using System.Collections;
 
 public class SoundEffectController : $$anonymous$$onoBehaviour 
 
 {
     
 
     public AudioSource footAudioSource;
     public AudioClip[] cementsteps;
     public AudioClip[] grasssteps;
     public AudioClip[] dirtsteps;
     public float stepInterval = 0.25f;
     private float nextStep = 0f;
 
     
 
     void OnTriggerEnter(Collider col)
     {
             if(col.tag == "pietra" && Time.time > nextStep)
         {
             nextStep = Time.time + stepInterval;
             footAudioSource.PlayOneShot(cementsteps[Random.Range(0, cementsteps.Length)]);
             return;
         }
             if(col.tag == "erba" && Time.time > nextStep)
         {
             nextStep = Time.time + stepInterval;
             footAudioSource.PlayOneShot(grasssteps[Random.Range(0, grasssteps.Length)]);
             return;
         }
             if(col.tag == "terra" && Time.time > nextStep)
         {
             nextStep = Time.time + stepInterval;
             footAudioSource.PlayOneShot(dirtsteps[Random.Range(0, dirtsteps.Length)]);
             return;
         }
 
     }
     
 }

Could you tell me it everything is ok as it seems?

Anyway, yes, there is a small sphere collider parented with each foot, and the different ground type are tagged differently and "flagged as trigger".

I'm going to post a video with an example!

avatar image grossim · Mar 23, 2013 at 05:15 PM 0
Share

Here's a video showing the script in action:

http://www.youtube.com/watch?v=G9ZrXcNuLPo

I've noticed an audio delay or latency after running for seconds...

Some advices to fix it? $$anonymous$$aybe setting a different stepInterval depending on character walk or run variables?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Play different sounds on different surfaces? 1 Answer

Music Zones play music with tags? 0 Answers

OnTriggerStay weird behaviour 0 Answers

Trying to play other objects' sound and animations from Raycast hit 1 Answer

Random footsteps 5 Answers


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