• 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 /
avatar image
0
Question by Cesurix · Feb 23 at 06:52 AM · scripting problemaudioscripting beginneraudioclipaudio.playoneshot

Hello eveyone, i want to make a footstep sound but my code doesn't make the audio work properly

Hello eveyone, i want to make a footstep sound but my code doesn't make the audio work properly It repeats itself for like infiite times when i press the "W" button. Here is my code:

         private void PlayFootStepAudio()
         {
             if (!m_CharacterController.isGrounded)
             {
                 return;
             }
             // pick & play a random footstep sound from the array,
             // excluding sound at index 0
             int n = Random.Range(1, m_FootstepSounds.Length);
             m_AudioSource.clip = m_FootstepSounds[n];
             m_AudioSource.PlayOneShot(m_AudioSource.clip);
             // move picked sound to index 0 so it's not picked next time
             m_FootstepSounds[n] = m_FootstepSounds[0];
             m_FootstepSounds[0] = m_AudioSource.clip;
         }


How can i a make this work properly?

Kind Regards, Cesurix

Comment
Add comment · Show 1
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 logicandchaos · Feb 23 at 01:34 PM 0
Share

you didn't provide all the code, but I assume you are using something like input keyDown so that if the key is held down the sound will start playing every frame.. you could do a check to see if the audio clip is already being played before you play it, although then you can no overlap the sound. At any rate I would recommend using a sound manager, there are some really good free ones on the asset store.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by pauldarius98 · Feb 24 at 08:50 PM

Thr problem is that most likely you run PlayFootStepAudio() in update and so as long as the character is on the ground, the audio source will play a footstep sound each frame. It depends on the game but if you have animation to the character, you could add 2 events on the animation (1 for each foot) to trigger the PlayFootStepAudio function and if not, you could run this code with a given delay between each step. I will attach the code for the second case

 private float lastPlayTime = 0f;
 private float delayBetweenSteps = 0.5f;
 
 private void PlayFootStepAudio()
 {
     if (!m_CharacterController.isGrounded || Time.time - lastPlayTime <= delayBetweenSteps)
     {
         return;
     }
 
     lastPlayTime = Time.time
     // pick & play a random footstep sound from the array,
     // excluding sound at index 0
     int n = Random.Range(1, m_FootstepSounds.Length);
     m_AudioSource.clip = m_FootstepSounds[n];
     m_AudioSource.PlayOneShot(m_AudioSource.clip);
     // move picked sound to index 0 so it's not picked next time
     m_FootstepSounds[n] = m_FootstepSounds[0];
     m_FootstepSounds[0] = m_AudioSource.clip;
 }

Comment
Add comment · Show 4 · 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 Cesurix · Feb 25 at 07:21 AM 0
Share

I tried your script and now my code works properly but i can't hear any footstep sounds:( The delay between two soundtrack works. Is there a problem with the Audio Source or do i make something silly? :) I can't figure out what is the problem. Is there anyone who can help me?

avatar image Jinkel92 Cesurix · Feb 25 at 11:30 AM 0
Share

Sorry, I have not taken into account the random audios.

avatar image pauldarius98 Cesurix · Feb 25 at 11:36 AM 0
Share

There can be a lot of cases. Make sure that you have an audio listener (usually attached to the current camera) And check the volume and the pitch of the audio source, where the audio source is positioned (should be on player), Volume Rolloff. If none of this works (aka are properly set) please provide more information with your audio setup (audio source, audio listener, etc)

avatar image Cesurix pauldarius98 · Feb 25 at 05:47 PM 0
Share

Thank you very much. I made something silly, the pitch was on -0.80. Thanks again for your kind answer:) Kind Regards, Cesurix

avatar image
0

Answer by Jinkel92 · Feb 25 at 11:43 AM

Well, you could change this:

 m_AudioSource.clip = m_FootstepSounds[n];
 m_AudioSource.PlayOneShot(m_AudioSource.clip);

For this:

  m_AudioSource.PlayOneShot(m_FootstepSounds[n].clip);

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 Cesurix · Feb 25 at 05:46 PM 0
Share

Thank you for trying to help me:) The problem was with the pitch in AudioSource. Kind Reagrds, Cesurix

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

241 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 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

Unitywebrequest returns fmod error 1 Answer

The volume does not reduce when master volume is lowered using volume slider 3 Answers

Looping an audio while holding "shift key" 1 Answer

Problems combining Audio triggers, PlayOneShot and Audio Arrays 1 Answer

Why PlayOneShot is not working? C# 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges