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 /
avatar image
0
Question by BeakerTeck · Oct 19, 2016 at 09:52 AM · audioprogramming

Only play audio if it isn't already -- if (!audio.isPlaying) not working

Hi,

I have some code that plays a random audio clip, randomly pitch shifted when I am walking.

Unfortunately it plays a new sounds EVERY frame so I get a jittery jumpy sound instead of nice clear footsteps.

Heres the code I am using:

 using UnityEngine;
 using System.Collections;
 
 public class footsteps : MonoBehaviour
 {
     public AudioClip[] footStepAudioClips;
     AudioSource footStepAudioSource;
  
     float randomFootStep;
     float randomPitch;
 
     void Start()
     {
         footStepAudioSource = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         // Check if we are walking
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
         {
             // Pick a random footstep sound to play
             randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
             footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];
 
             // Pick a random pitch to play it at
             randomPitch = Random.Range(-3, 3);
             footStepAudioSource.pitch = (int)randomPitch;
 
             // Play the sound
             footStepAudioSource.Play();
         }
     }
 }


I have tried wrapping it in an

    if (!footStepAudioSource.isPlaying) {
             // Pick a random footstep sound to play
             randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
             footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];
 
             // Pick a random pitch to play it at
             randomPitch = Random.Range(-3, 3);
             footStepAudioSource.pitch = (int)randomPitch;
 
             // Play the sound
             footStepAudioSource.Play();
    }

But sadly it doesn't effect anything.

Any help appreciated.

Comment
Add comment · Show 10
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 JoshDangIt · Oct 19, 2016 at 06:20 PM 0
Share

Not sure why audio.isPlaying isn't working. Have you tried adding a timer to see if the time since it last played a footstep is greater than footStepAudioSource.clip.length?

avatar image BeakerTeck JoshDangIt · Oct 19, 2016 at 09:25 PM 0
Share

Will have a go at that. Just need to work out how :)

avatar image Dibbie · Oct 19, 2016 at 06:49 PM 0
Share

Im not sure why, but sometimes relying on ! doesnt always work, but printing out the full thing footStepAudioSource.isPlaying == false works sometimes, and the same with bool conditions - I have no idea, but that often solves most issues I ever have with those issues.

avatar image BeakerTeck Dibbie · Oct 19, 2016 at 09:24 PM 0
Share

Hi thanks for that, I changed it but still no effect

avatar image Naphier Dibbie · Oct 20, 2016 at 02:59 AM 0
Share

That's not true at all. If this has worked for you then you're doing something else wrong.

avatar image Adamcbrz · Oct 19, 2016 at 07:45 PM 1
Share

What part of the code are you wrapping? If you do it after you set the clip then that might be the problem.

avatar image BeakerTeck Adamcbrz · Oct 19, 2016 at 09:23 PM 0
Share

edited question to reflect this, but i wrapped everything inside the movement check.

avatar image Adamcbrz BeakerTeck · Oct 19, 2016 at 11:13 PM 0
Share

How long is the audio clip?

Show more comments
avatar image Naphier · Oct 20, 2016 at 03:05 AM 1
Share

AudioSource.isPlaying should absolutely work. If not, maybe it is some editor glitch or bug. Test it out to doubly make sure. Add a public bool to your class and every frame set that = to footStepAudioSource.isPlaying then watch it in the editor. There can be issues if you use PlayOneShot(), but you're not. Alternatively, use a coroutine with a timer. Just yield return new WaitfForSeconds(footStepAudioSource.clip.length). Use a bool to see if the Coroutine is running or has finished.

2 Replies

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

Answer by BeakerTeck · Oct 20, 2016 at 08:01 AM

Thanks to @JoshDangIt for the timer idea, and @Naphier for the Coroutine implementation idea.

Below is the entire code as now implemented.

 using UnityEngine;
 using System.Collections;
 
 public class footsteps : MonoBehaviour
 {
     public AudioClip[] footStepAudioClips;
     AudioSource footStepAudioSource;
  
     float randomFootStep;
     float randomPitch;
 
     bool playing;
 
     void Start()
     {
         footStepAudioSource = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         // Check if we are walking
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
         {
             if (playing == false)
             {
                 StartCoroutine("playFootStep");
             }
         }
     }
 
     IEnumerator playFootStep()
     {
         playing = true;
         // Pick a random footstep sound to play
         randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
         footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];
 
         // Pick a random pitch to play it at
         randomPitch = Random.Range(1, 3);
         footStepAudioSource.pitch = (int)randomPitch;
 
         // Play the sound
         footStepAudioSource.Play();
         yield return new WaitForSeconds(footStepAudioSource.clip.length);
         playing = false;
     }
 
 }
Comment
Add comment · 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
0

Answer by JoshDangIt · Oct 20, 2016 at 04:22 PM

Don't know why I didn't think of this before, but a better and more realistic way of handling footsteps is to use Animation Events. https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

AudioSource.GetSpectrumData Failure 0 Answers

Wwise Integration: Where do i start Coding? 1 Answer

How can I play a sound every time a particle is emitted? 3 Answers

play sound when moving stop sound when not 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