Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question overlapping fire for gunshot sound

Discussion in 'Editor & General Support' started by youIsArent, Aug 28, 2022.

  1. youIsArent

    youIsArent

    Joined:
    Dec 13, 2021
    Posts:
    9
    i am attempting to implement a gunshot sound to go alongside a gun firing and animation, the gunshot sound is slightly longer than the time it takes for me to fire the gun again; i am asking if and how it would be possible for me to use the sound multiple times without cutting off the first sound with the second.
    code if it matters:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pistolFire : MonoBehaviour
    6. {
    7.     public GameObject blackPistol;
    8.     public bool isFiring = false;
    9.     public GameObject muzzleFlash;
    10.     public AudioSource pistolShot;
    11.  
    12.     void Update()
    13.     {
    14.         if(Input.GetButtonDown("Fire1"))
    15.         {
    16.             if (isFiring == false)
    17.             {
    18.                 StartCoroutine(firePistol());
    19.             }
    20.         }
    21.     }
    22.  
    23.     IEnumerator firePistol() {
    24.          isFiring = true;
    25.         blackPistol.GetComponent<Animator>().Play("firePistol");
    26.         pistolShot.Play();
    27.         muzzleFlash.SetActive(true);
    28.         yield return new WaitForSeconds(0.015f);
    29.         muzzleFlash.SetActive(false);
    30.         yield return new WaitForSeconds(0.13f);
    31.         blackPistol.GetComponent<Animator>().Play("New State");
    32.         isFiring = false;
    33.  
    34.     }
    35. }
     
  2. bobby55

    bobby55

    Joined:
    Jun 24, 2020
    Posts:
    48
    PlayOneShot is most commonly used https://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html
    but it can produce clipping and i think i've heard it's slightly inefficient.

    you could try create a sound file with the overlap already applied - of course this isn't ideal and i'm not sure it would work

    if its only a small delay you could try using 2 audio sources instead of one with GetComponents https://docs.unity3d.com/ScriptReference/GameObject.GetComponents.html and then overlap both of them with a delay
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    One cheap and cheesy way is to take that AudioSource and clone it a few times (at
    Start()
    time), then keep those extra copies in an array that you rotate through each shot.

    This lets you also pitch the sound up and down slightly, or change the volume slightly so it doesn't sound super-ear-fatigueing.
     
  4. youIsArent

    youIsArent

    Joined:
    Dec 13, 2021
    Posts:
    9
    yo i'm like super new (i only know javascript) can you like tell me how to do this in C#?