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 /
avatar image
1
Question by BritishGuy · Jan 12, 2014 at 11:09 AM · audiosoundaudiosourcemultipletank

Buzzing Audio

Hello i am having problems with unity audio-source, For some reason my audio is buzzing.

There are 2 audio-sources on a tank, The Engine and Tracks. The engine plays on awake then loops then the pitch of the engine is changed in script, The tracks i want to play when (speed > 1) But for some reason the tracks still play when stationary "not intended" but the tracks audio plays and sounds normal, When i start to drive forward the tracks audio clip starts to buzz violently until my speed is at 0 again. I cannot make sense of it but i hope someone can help me. Here is the code.

EDIT: Here is a webplayer so you can see / hear the problem i am facing. Thanks.WebPlayer

 public class TransformSpeed : MonoBehaviour
 {
     //Define the Variables
     Vector3 lastPosition = Vector3.zero;
     public float Speed = 0;
     float maxSpeed = 25;
 
     AudioSource tankEngine;
     AudioSource tankTracks;
 
     void Start() {
         AudioSource[] audios = GetComponents<AudioSource>();
         tankEngine = audios[0];
         tankTracks = audios[1];
     }
 
     void FixedUpdate()
     {
 
         
         Speed = (transform.position - lastPosition).magnitude / Time.deltaTime * 3.6f;
         
         //don't forget to update your lastposition after the calculation
         lastPosition = transform.position;
     }
     
     void OnGUI()
     {
         // a simple way to display the speed in km/h of the Transform Object
         GUI.Label(new Rect(10, 10, 100, 50), Speed.ToString("##0 km/h"));
         
 
         if (Speed > 0){
             tankEngine.audio.pitch = 0.5f + (Speed / maxSpeed);
         }
         if (Speed > 1){
             tankTracks.Play();
         }
     }
 }


Comment
Add comment · Show 5
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 BritishGuy · Jan 12, 2014 at 12:48 PM 0
Share

Anybody have any rough ideas to how i can prevent this? Any information would be greatly appreciated, Thanks.

avatar image KellyThomas · Jan 12, 2014 at 01:24 PM 0
Share

"The engine plays on awake..." This script has no Awake() method. Is there another script that is also managing the sound?

avatar image Gruffy · Jan 12, 2014 at 01:56 PM 0
Share

I thnk your audio needs to be set as a 2d audio clip. Go to your Audio clip and in it inspector panel untick the box marking it as a 3D audio clip/sound. This will make it unaffected by any doppler. Could be wrong here, just sounds like you have an audio track that plays as a 3D sound in my limited experience. Cheers bud. gruffy

avatar image BritishGuy · Jan 12, 2014 at 02:36 PM 0
Share

@$$anonymous$$elly$$anonymous$$

The engine sound plays on awake from the GameObjects AudioSource as it will always be playing that clip, So didn't need to call it from script for now.

@Gruffy

3D Audio clips can also be unaffected by Doppler by turning down the Doppler slider in the inspector 3D Sound settings.

avatar image Gruffy · Jan 12, 2014 at 02:43 PM 0
Share

Hey bud, you are correct but I guess I was, more over, saying that if you need the sound to travel with you it should most oikely be set to a 2d audio clip as a matter of course. However, im basing this on best practices as I dont realky know what you are developing etcb. Anyway, thanks for getting back dude. Take care Gruffy

1 Reply

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

Answer by KellyThomas · Jan 12, 2014 at 01:34 PM

It sounds like you are starting the clip from the start every time you call AudioSource.Play(). This would explain a buzzing when Speed is greater than 1.

You can use `AudioSource.isPlaying` to correct this behavior. There is a nice example on that manual page.

On a side note I would recommend moving your audio logic out of OnGUI() and into Update(). I believe that ``OnGUI()` is called multiple times per frame, and you probably don't need to execute this logic so often.

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 BritishGuy · Jan 12, 2014 at 02:30 PM 0
Share

Thanks for your help, I changed a line from

 if (Speed > 1){
             tankTracks.Play();

to

 if (Speed > 1 && !tankTracks.isPlaying){
             tankTracks.Play();

And also moved everything to Update() Thanks again.

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

20 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

Related Questions

Multiple audioSources on same gameObject? 1 Answer

multiple sounds, sound....sucky 0 Answers

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

Intensity of sound. -1 Answers

Using Distance from AudioSource 0 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