Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Audio crackling after sound effect

Discussion in '2D' started by Marcel-Kaemper, Oct 28, 2018.

  1. Marcel-Kaemper

    Marcel-Kaemper

    Joined:
    Oct 20, 2018
    Posts:
    9
    Hi,
    I added a shoot sound effect to my game, but unfortunately I always hear a crackling when the sound effect ends. The audio file it self is clear, so the crackling comes definitely from unity.
    The code which is supposed to play the sound looks like this:
    Code (CSharp):
    1. public AudioClip clip;
    2.  
    3. private AudioSource source;
    4.  
    5. void Start(){
    6. source = GetComponent<AudioSource>();
    7. }
    8. void Update(){
    9. source.PlayOneShot(clip);
    10. }
    I also recorded it, make sure to turn the volume up:
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you are playing the sound every frame, play it once at the needed place

    Code (CSharp):
    1. //if this script is on bullet, then
    2. void Start(){
    3. source = GetComponent<AudioSource>();
    4. source.PlayOneShot(clip);
    5. }
    or at the place, where you are spawn the bullet
    {Instantiate....
    playoneshot}
     
  3. Marcel-Kaemper

    Marcel-Kaemper

    Joined:
    Oct 20, 2018
    Posts:
    9
    I actually play the sound when I spawn the bullet, I just wanted to keep the code in my post as short as possible.
    Here's the whole code:
    Code (CSharp):
    1. public class shoot : MonoBehaviour {
    2.  
    3.     public GameObject projectile;
    4.     public GameObject gunPoint;
    5.  
    6.     public Animator anim;
    7.  
    8.     public AudioClip clip;
    9.  
    10.     public float speed;
    11.     public float cooldown;
    12.     public float bulletTime;
    13.  
    14.     private AudioSource source;
    15.  
    16.     private bool canShoot = true;
    17.  
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         source = GetComponent<AudioSource>();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.        
    27.         if(Input.GetKeyDown(KeyCode.Space) && canShoot)
    28.         {
    29.             GameObject go = (GameObject) Instantiate(projectile, (Vector2)gunPoint.transform.position, Quaternion.identity);
    30.  
    31.             go.GetComponent<Rigidbody2D>().transform.Translate(Vector3.forward);
    32.             go.GetComponent<Rigidbody2D>().AddForce(transform.up * speed);
    33.  
    34.             anim.SetBool("shooting", true);
    35.  
    36.             source.PlayOneShot(clip);
    37.  
    38.             StartCoroutine(CanShoot());
    39.             StartCoroutine(RemoveObject(go));
    40.         }
    41.     }
    42.     IEnumerator CanShoot()
    43.     {
    44.         canShoot = false;
    45.         yield return new WaitForSeconds(cooldown);
    46.         anim.SetBool("shooting", false);
    47.         canShoot = true;
    48.     }
    49.  
    50.     IEnumerator RemoveObject(Object obj)
    51.     {
    52.         yield return new WaitForSeconds(bulletTime);
    53.         Destroy(obj);
    54.     }
    55.  
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    can it be that audioclip isn't at zero volume at the end ? You can try to adjust audioclip with an extra audioeditor (like audacity
     

    Attached Files:

  5. Marcel-Kaemper

    Marcel-Kaemper

    Joined:
    Oct 20, 2018
    Posts:
    9
    That did it... Thank you!!