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
0
Question by TKDR · Oct 11, 2021 at 06:35 PM · audio.playoneshot

AudioSource.PlayOneShot strangely high-pitched

Hi! I'm currently working on a vehicle controller that I plan on releasing on the Asset Store relatively soon. I'm in the middle of some polishing right now, working on bug fixes, detail audio, examples etc. I discovered something rather strange with one of my audio effects. I'm using AudioSource.PlayOneShot to play "thud" noises when the suspension on a vehicle experiences sudden, fast compression—such as when going over a speed bump or up a curb. 99% of the time the sound acts as expected, with the correct timing, pitch, volume, and location, but every once in a while the OneShot will play with a very high pitch. This sounds completely out of place, and I haven't the slightest clue as to what causes it. At first I thought it might be because I was playing very many OneShots in quick succession, causing it to behave differently, or causing Unity to reach its default limit of 32 (I think?) real voices, but after adding in an adjustable delay between OneShots, I'm not seeing (or rather hearing) any improvement. I also have randomized pitch variation on these OneShots, but that isn't what's at fault, as even when I set the min and max values to the same number I still experience the issue. Just to get an idea of how much higher-pitch than intended the OneShot's clip is sometimes played, I did some comparisons, by ear (meaning far from perfect), and I think it's between 7 and 10 times the intended pitch. I also tried printing out the pitch every frame to detect any odd anomalies, but the pitch variable (on the AudioSource I mean) never leaves the intended range. Here's the content of my script:

     [Header("References")]
     public Vehicle vehicle;
     public AudioSource source;
     public AudioClip[] clips;

     [Header("Compression")]
     [Tooltip("The speed in m/s of the wheel along the axis of its suspension to trigger a compression noise")]
     public float minCompressionSpeed = 1f;
     public float maxSpeed = 6f;
     public float volumeAtMin = 0f;
     public float volumeAtMax = 1f;
     public float minPitch = 0.9f;
     public float maxPitch = 1.1f;
     [Tooltip("The smallest time allowed between compression sounds being made")]
     public float minTimeBetweenCompressions = 0.1f;
     private float timeSinceLastCompression = 10f;

     private void Awake()
     {
         if (vehicle == null) vehicle = GetComponentInParent<Vehicle>();
         if (source == null) source = GetComponentInChildren<AudioSource>();
         source.playOnAwake = false;
     }

     private void FixedUpdate()
     {
         float highestSpeed = 0f;
         int index = 0;
         for (int i = 0; i < vehicle.wheels.Count; i++)
         {
             Wheel wheel = vehicle.wheels[i];
             float speed = (wheel.compression - wheel.previousCompression) / Time.fixedDeltaTime * wheel.radiusFactor;
             if (speed > highestSpeed)
             {
                 highestSpeed = speed;
                 index = i;
             }
         }

         if (highestSpeed > minCompressionSpeed && timeSinceLastCompression > minTimeBetweenCompressions)
         {
             float t = (highestSpeed - minCompressionSpeed) / (maxSpeed - minCompressionSpeed);
             float volume = Mathf.Lerp(volumeAtMin, volumeAtMax, t);
             source.transform.position = vehicle.wheels[index].transform.position;
             source.pitch = Mathf.Lerp(minPitch, maxPitch, Random.value);
             AudioClip clip = JoyUtils.RandomClip(clips);
             source.PlayOneShot(clip, volume);
             timeSinceLastCompression = 0f;
         }

         timeSinceLastCompression += Time.fixedDeltaTime;
     }
 }

I haven't found anyone else experiencing an identical issue, which makes me think it's something wrong with my code rather than Unity, but idk.

Thanks in advance for any replies!

Oh, and one last thing. It's a commonly accepted myth that PlayOneShot creates a new AudioSource and then destroys it, generating garbage. The falsity of this claim has been proven by several other people on the forums. PlayOneShot() is actually very performant, even compared to Play(), and especially compared to PlayClipAtPoint() which actually does instantiate a new source, and then destroy it, generating garbage. Just thought I'd inform y'all about that and point out that it cannot be responsible for the issue I'm experiencing.

This is also the first time in a very long time (or maybe ever) that I'm posting here, so I apologize if there's anything that goes against convention in this post. If so, it's purely unintentional.

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

0 Replies

· Add your reply
  • Sort: 

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

125 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

Related Questions

Audio not playing when OnTriggerEnter2D 2 Answers

Triggered audio clip deactivates audio snapshot background music - are audio clips and snapshots not able to work simultaneously? 0 Answers

Why do AudioClips still play with no Audiolistener? 0 Answers

Play sound effect throughtout an entire animation clip 1 Answer

C# Wait For Seconds 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