- Home /
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 ?
Answer by superventure · Feb 26, 2011 at 10:38 PM
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();
i have tried this but still get just sound at the start and not when im shooting ?
Answer by save · Mar 31, 2012 at 04:45 PM
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);
}
Answer by ivan1233 · Feb 27, 2013 at 09:56 PM
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.