• 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
Question by jfarzan · Jul 24, 2013 at 09:35 PM · audiosoundaudiosourceaudioclip

AudioSource won't play

I'm trying to program footsteps for my player character. I'm doing t$$anonymous$$s by attac$$anonymous$$ng a simple boolean to the Headbob script: when the head is bobbing (ie the character is moving), play the clip, otherwise don't. The script is attached to the main camera, w$$anonymous$$le the source is attached to the player, so I've been using ((AudioSource)GameObject.Find("~player~").GetComponent("AudioSource")) to first access the AudioSource and then use it with either audio.Play() or audio.Pause(). However, I just can't get it to work. The most I get is a random muffled tap every so often, or a very very faint and constant sound, but obviously neither of these are footsteps. I've even used Debug.Log(((AudioSource)GameObject.Find("~player~").GetComponent("AudioSource")).audio.isPlaying) to try and figure out my problem, but it returns true at all the appropriate times. Any suggestions?

UPDATE Upon further testing, it seems that the footsteps are playing, they just sound incredibly, incredibly distorted, like the player is running on uneven bubblewrap as opposed to, you know, walking.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 public class Headbobber: MonoBehaviour 
 {
  
   public float moveSpeed = 1.5F;
   private Vector3 moveDir = new Vector3(0, 0.1F, 0);
   private Vector3 initPos, movement;
   private float moveTime = 0;
   private float twoPI = Mathf.PI * 2;
   public bool isBobbing = false;
 
   void Start()
   {
     initPos = transform.localPosition;
   }
  
    void Update () 
    {
     Debug.Log(((AudioSource)GameObject.Find("~player~").GetComponent("AudioSource")).audio.isPlaying);
       if(((navigation)GameObject.Find("~player~").GetComponent("navigation")).isMoving == true)
       {
         isBobbing = true;
         moveTime += Time.deltaTime * moveSpeed;
         movement = transform.TransformDirection(moveDir) * Mathf.Sin(twoPI * moveTime);
         transform.localPosition = initPos + movement;
         ((AudioSource)GameObject.Find("~player~").GetComponent("AudioSource")).audio.Play();
       }
       else
       {
         isBobbing = false;
         ((AudioSource)GameObject.Find("~player~").GetComponent("AudioSource")).audio.Pause();
         
       }
    }
  
  
 }
Comment

People who like this

0 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 LemonLake · Jul 26, 2013 at 09:53 AM 0
Share

By distorted, do you mean the pitch? Perhaps you could set the Doppler Factor to 0.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jul 24, 2013 at 10:38 PM

I believe your problem is that you are calling 'audio.Play()' every frame, even if the audio clip is already playing. You might be able to fix t$$anonymous$$s just by only calling it if AudioSource.isPlaying is false. There are a few other issues here as well. You don't want to call GameObject.Find() or even GetComponent() every frame if you can avoid it. Also, since the Audio source is on the player, not the camera, you don't need to require an AudioSource component on t$$anonymous$$s game object. Here is a bit of cleanup and rewrite. I'm using InvokeRepeating() to time the footsteps, but you could use a timer or even the isPlaying check outlined above (untested):

 using UnityEngine;
 using System.Collections;
  
 public class Headbobber: MonoBehaviour 
 {
     public float moveSpeed = 1.5F;
     private Vector3 moveDir = new Vector3(0, 0.1F, 0);
     private Vector3 initPos, movement;
     private float moveTime = 0;
     private float twoPI = Mathf.PI * 2;
     private GameObject goPlayer;
     private navigation nav;
     private AudioSource aud;
     
     void Start()
     {
         initPos = transform.localPosition;
         goPlayer = GameObject.Find("~player~");
         nav = goPlayer.Getcomponent(navigation);
         aud = goPlayer.audio;
         InvokeRepeating ("PlayFootstep", 0.0f, 0.75f);
     }
     
     void PlayFootstep()
     {
         if (nav.isMoving)
             aud.Play();
     }
     
     void Update () 
     {
         Debug.Log(aud.isPlaying);
         if(nav.isMoving)
         {
             moveTime += Time.deltaTime * moveSpeed;
             movement = transform.TransformDirection(moveDir) * Mathf.Sin(twoPI * moveTime);
             transform.localPosition = initPos + movement;
         }
 
     }
 }
Comment
clunk47
shakedniss

People who like this

2 Show 3 · 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 jfarzan · Jul 24, 2013 at 11:19 PM 0
Share

I tried this out, and while it successfully got the audio to play correctly, it only played correctly when the character was stopped. When the character moved, the audio tended to skip. I do appreciate your cleaning up my code, though!

avatar image shimauma · Jul 25, 2013 at 12:23 AM 2
Share

Expanding on robertbu's answer, change the PlayFootstep() method to something like this:

 void PlayFootstep()
 {
 if (nav.isMoving && !aud.isPlaying) // toggle sound on when you start moving
    aud.Play();
 else if (!nav.isMoving && aud.isPlaying) // toggle sound off when you stop moving
    aud.Pause();
 }

avatar image jfarzan · Jul 25, 2013 at 02:29 PM 0
Share

That seems to have gotten the job done, thank you so much!

avatar image

Answer by tw1st3d · Jul 24, 2013 at 10:17 PM

Try setting an AudioSource variable as

 public AudioClip theClip = ((AudioSource)GameObject.Find("~player~").GetComponent("AudioSource"));
 theClip.Play(0); 
Comment
clunk47

People who like this

-1 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 clunk47 · Jul 25, 2013 at 12:26 AM 0
Share

You cannot implicitly convert an AudioSource to an AudioClip.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

PlayOneShot returns false for isPlaying 5 Answers

Play sound from an array, not random, and only once 3 Answers

How to get decibel's value of game's sound? 0 Answers

Audio: -3db automatic attenuation on any audio playing? 0 Answers

Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 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