- Home /
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.
Your answer
Follow this Question
Related Questions
Audio not playing when OnTriggerEnter2D 2 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