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

automatic rifle sound plays once

Discussion in 'Audio & Video' started by camlam999, Jan 3, 2015.

  1. camlam999

    camlam999

    Joined:
    Aug 8, 2014
    Posts:
    29
    so I have been working on this game and I need this gun to make a sound but if I add a sound I know I can just make it semi-automatic and play sound after sound but I need it to be fully automatic and its really pissing me off because if I just fire it once with sound clip it plays fine but if I hold it it makes a high pitch spastic sound
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,300
    How are you playing the sounds?
    Your code must be wrong.
     
  3. camlam999

    camlam999

    Joined:
    Aug 8, 2014
    Posts:
    29
    i never coded in for it to make sound
    my code:
    Code (JavaScript):
    1. var firing : boolean = false;
    2. var accuracy : float;
    3. private var bulletCaseGenerator : Transform;
    4. private var bulletTraceGenerator : Transform;
    5. private var muzzleFlashGenerator : Transform;
    6. private var bulletCaseGeneratorScript : bulletCaseGenerator;
    7. private var bulletTraceGeneratorScript : bulletTraceGenerator;
    8. private var muzzleFlashGeneratorScript : muzzleFlashGenerator;
    9.  
    10. function Start(){
    11.     bulletCaseGenerator = transform.Find("bulletCaseGenerator");
    12.     bulletTraceGenerator = transform.Find("bulletTraceGenerator");
    13.     muzzleFlashGenerator = transform.Find("muzzleFlashGenerator");
    14.     bulletCaseGeneratorScript = bulletCaseGenerator.GetComponent("bulletCaseGenerator");
    15.     bulletTraceGeneratorScript = bulletTraceGenerator.GetComponent("bulletTraceGenerator");
    16.     muzzleFlashGeneratorScript = muzzleFlashGenerator.GetComponent("muzzleFlashGenerator");
    17.     firing = false;
    18.     //accuracy = 0.9;
    19. }
    20.  
    21. function Update () {
    22.     bulletCaseGeneratorScript.on = firing;
    23.     bulletTraceGeneratorScript.on = firing;
    24.     muzzleFlashGeneratorScript.on = firing;
    25.     bulletTraceGeneratorScript.accuracy = accuracy;
    26.     firing = false;
    27. }
    28.  
    29. function Fire(){
    30.     firing = true;
    31. }
    32.  
    33. function SetAccuracy(accuracyValue : float){
    34.     accuracy = accuracyValue;
    35. }
     
  4. bloeys

    bloeys

    Joined:
    Aug 26, 2014
    Posts:
    57
    In your shoot function, where you shoot your bullets, add the code to play your sound there. that way every time you shoot the sound will be played.
     
  5. camlam999

    camlam999

    Joined:
    Aug 8, 2014
    Posts:
    29
    that is what i did but since it is a case of you hold it it is a automatic rifle every time you hold it down it makes a spastic sound
     
  6. bloeys

    bloeys

    Joined:
    Aug 26, 2014
    Posts:
    57
    Do you mean that the sound is started over and over very fast making it bad?
    If that's the case, then before you play the sound make a check to see if the shooting sound isn't playing, it is isn't then play, otherwise don't play it for that shot.