x


Audio when I shoot

How do I add a sound when I fire a bullet with my weapon ? I've tried to add ''Audio Source'' but the sound just come automatically when I start.

Anyone know How I can add the sound when I shoot ?

more ▼

asked Feb 26 '11 at 10:34 PM

simpleone gravatar image

simpleone
110 36 40 46

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Sure, drag a sound file to the object, and in the audio source drop list, uncheck play on awake. To play sound in script,

var shot: AudioClip; 

 audio.clip = shot;

    audio.Play();
more ▼

answered Feb 26 '11 at 10:38 PM

superventure gravatar image

superventure
634 44 53 63

i have tried this but still get just sound at the start and not when im shooting ?

Mar 31 '12 at 02:56 PM metalmutha11
(comments are locked)
10|3000 characters needed characters left

Hey simpleone, i see that you want something simple :D (beacuse of name)...

1) You need to attach audio file to a player.

2) Uncheck Play On Awake.

3) Make JavaScript.

4) Copy this code to your script...

function Update() {

audio.Play(); //Script plays the audio file that you attached...
// shot code...

}

5) Attach script to a player.

6) Test and enjoy!

Good Luck, Ivan.

more ▼

answered Feb 27 at 09:56 PM

ivan1233 gravatar image

ivan1233
1 1

(comments are locked)
10|3000 characters needed characters left

Here is a more complete example, feel free to ask if you have any questions. Add this script to the gun. Make sure that the gun has an AudioSource component attached in the Inspector (this will happen automatically if you drag and drop your AudioClip to a GameObject in the scene) and that you have unchecked "Play On Awake".

Example 1: The cut-off

#pragma strict

@script RequireComponent(AudioSource) 

var fireRate : float = 1.0; //The rate of fire for the gun

private var nextFire : float = 0.0; //When the next fire is ok to shoot off

function Update () {
    if (Input.GetButton("Fire1") && IsOkToShoot()) Shoot(); //Shoot when conditions are met
}

function IsOkToShoot () : boolean {

    //Here we check that current time exceeds nextFire

    var itsOk : boolean = false;
    if (Time.time>nextFire) {
       nextFire = Time.time + fireRate;
       itsOk = true;
    }
    return itsOk;
}

function Shoot () {

    audio.Play(); //Play the AudioClip on this AudioSource

    /* Here you could also add animations and your other logic for shooting */

}

Remember that you also have audio.isPlaying (which is a boolean for checking if the AudioClip on the current AudioSource is playing). But with a fire rate-check you most likely won't need that as you want to be able to play the sound directly when the conditions for shooting are met.

When developing for desktop or devices that doesn't demand that you cache everything for performance (read iOS/Android) you most likely would like a more transparent approach where audio won't be cut off. This is easiest done with PlayClipAtPoint where a GameObject with an AudioSource attached will instantiate on every shot. This only requires that you have the AudioClip dragged and dropped to the variable Shot in the Inspector.

Example 2: The transparent

#pragma strict

var shot : AudioClip;
var fireRate : float = 1.0;
private var nextFire : float = 0.0;
private var t : Transform;

function Start () {
    t = transform;
}

function Update () {
    if (Input.GetButton("Fire1") && IsOkToShoot()) Shoot();
}

function IsOkToShoot () : boolean {
    var itsOk : boolean = false;
    if (Time.time>nextFire) {
       nextFire = Time.time + fireRate;
       itsOk = true;
    }
    return itsOk;
}

function Shoot () {
    AudioSource.PlayClipAtPoint(shot, t.position);
}
more ▼

answered Mar 31 '12 at 04:45 PM

save gravatar image

save
8.2k 22 31 62

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1027
x523
x329
x308
x151

asked: Feb 26 '11 at 10:34 PM

Seen: 3207 times

Last Updated: Feb 27 at 09:56 PM