- Home /
audio.PlayOneShot Lag !
Hello , Is there any fix to audio.PlayOneShot ? When the sound play , it play too slow :( Any idea ?
using UnityEngine;
using System.Collections;
public class DestroyByScore : MonoBehaviour {
public Score scoreScript;
public AudioClip AmerFX;
void Start()
{
//Find Scripts GameObject, and get Score Component.
scoreScript = GameObject.Find ("Score").GetComponent<Score>();
}
void Update()
{
if(scoreScript != null && scoreScript.score >= 100)
Destroy (gameObject);
audio.PlayOneShot(AmerFX);
}
}
Thanks in advance!:)
Answer by AndyMartin458 · Nov 15, 2013 at 10:34 PM
If the audio is a compressed format like .mp3 or .ogg, that will always take longer to play. Make sure you are using .wav for any sound effects.
Thank you . It's a good point :) . The Solution was using AudioSource.PlayClipAtPoint The final script should be like this : using UnityEngine; using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Score : $$anonymous$$onoBehaviour
{
public int score = 0; // The player's score.
public GameObject Amer;
public AudioClip AmerFX;
private PlayerControl playerControl; // Reference to the player control script.
private int previousScore = 0; // The score in the previous frame.
void Awake ()
{
// Setting up the reference.
playerControl = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerControl>();
}
void Update ()
{
// Set the score text.
guiText.text = "Score: " + score;
// If the score has changed...
if(previousScore != score)
// ... play a taunt.
playerControl.StartCoroutine(playerControl.Taunt());
// Set the previous score to this frame's score.
previousScore = score;
if(score == 100){
AudioSource.PlayClipAtPoint(AmerFX, new Vector3(5, 1, 2));
Destroy(Amer,5);
}
}
}
@Amer_ALhwifi I didn't realize that you were using a 3D audio source. So the problem had to do with the time it took the audio to travel to your player?
I am trying to load an .ogg vorbis file with playOneShot and it is taking about a second just to play a quick preloaded 2kb sound... Is this normal? Is there a way around this other than using .wav?
That is normal. You'll need to use an uncompressed format.
In my case it turned out to be 'invoke' that was doing the lag. Now my sounds play just fine.
Your answer
Follow this Question
Related Questions
Lag/jerkiness (as opposed to low framerate) in a Rhythm game 2 Answers
AudioClip huge lag on some android devices 0 Answers
Sound Cracks or Lags on ZTE Blade? 0 Answers
Music Choppy and Weird 0 Answers
Audio Lag Between Multiple Listeners 0 Answers