Search Unity

Audio Inconstant audio delay

Discussion in 'Audio & Video' started by Emilyanis, Feb 1, 2019.

  1. Emilyanis

    Emilyanis

    Joined:
    Feb 22, 2014
    Posts:
    48
    Hi!

    I am making game with a lot of weapons in it. The sound is very important part of weapon feel. Unfortunately, I cant overcome inconstant audio delay between shots of the weapon.

    Here is example:

    issue.png

    This is how its works:
    "Gun" script triggers "Sound Emitter" script to play audio. "Sound Emitter" shuffles audio clips, pick random and play it. The delay in "Gun" script in frames, frames count and input is in FixedUpdate.

    Code (CSharp):
    1.  
    2.     public SoundEmitter sound;
    3.     public float rpm;
    4.     private int fireDelay;
    5.     private int nextFire;
    6.  
    7.     void Awake()
    8.     {
    9.         rpm = 60f / rpm;
    10.         fireDelay = TimeToFrames(rpm);
    11.     }
    12.  
    13.     void FixedUpdate()
    14.     {
    15.         if (nextFire > 0)
    16.             nextFire--;
    17.  
    18.         if (Input.GetMouseButton(0) && nextFire <= 0)
    19.             Fire();
    20.     }
    21.  
    22.     void Fire()
    23.     {
    24.         nextFire += fireDelay;
    25.         sound.Play();
    26.     }
    27.  
    28.     int TimeToFrames(float _time)
    29.     {
    30.         return Mathf.RoundToInt(_time / Time.fixedDeltaTime);
    31.     }
    I tried optimize both scripts and tried simplify them, but none of that worked. Changing the audio import options not working, as is Audio Settings.

    You can try it for yourself. Even in empty scene delay is hearable. LMB to shoot.
    https://drive.google.com/file/d/1y9t4zvtq9oOI_p3P2ZabPZsSRT6RQ-Lo/view?usp=sharing

    Thanks!
     
  2. Crippley

    Crippley

    Joined:
    Jul 7, 2022
    Posts:
    1
    Hey there @Emilyanis , did you manage to solve this issue and if so, would you mind telling me how?