x


Adding sound to MachineGun

I've read and followed the tutorial for adding sound. This is what my Hierarchy looks like.

Weapons Machinegun Machinegun muzzle_flash sparks

I added a machinegun script to the empty machinegun game object. I have tried adding an audio source to my machinegun prefab, my sparks, and my muzzle_flash. If I add it to my sparks I get sound only if I mark Play On Awake, but then the machinegun sound clip just keeps firing. If I unmark the PLay on Awake, I get no sound. If I add it to muzzle_flash I get the same results.

How do I get the machinegun audio clip to play only when I'm pressing down the (Fire button)mouse button or the ctr button? What is the exact procedure? I'm obviously doing something wrong or I done something wrong. Can you help me. Thanx Troy

more ▼

asked Jan 23 '11 at 04:40 AM

trimtro gravatar image

trimtro
1 4 4 4

Look at the FPS Tutorial

Jan 23 '11 at 05:19 AM fireDude67
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

I would look into using OnKeyDown to fire Play() and OnKeyUp to fire Stop()

more ▼

answered Jan 23 '11 at 09:30 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

(comments are locked)
10|3000 characters needed characters left
var gunSound : AudioClip;  

  if(Input.GetButtonDown("Fire1"))
    {
    audio.Play("gunSound");
    }
more ▼

answered Jan 23 '11 at 12:11 PM

OrangeLightning gravatar image

OrangeLightning
5.4k 47 57 112

how do you stop audio when mouse button is released

Nov 10 '12 at 04:09 PM Skyline Studios
(comments are locked)
10|3000 characters needed characters left

how do you do this for the mouse when clicked?

more ▼

answered Sep 13 '11 at 05:28 AM

GameCreater gravatar image

GameCreater
1 1 2 2

Instead of

var gunSound : AudioClip;

if(Input.GetButtonDown("Fire1")) { audio.Play("gunSound"); }

you would do:

var gunSound : AudioClip;

if(Input.GetMousebuttonDown(0)) { audio.Play("gunSound");

}

That is for the left mouse button. If you wanted the right mouse button you would do GetMouseButtonDown(1).

Jan 19 '12 at 02:47 AM Camando360
(comments are locked)
10|3000 characters needed characters left

Here's what I did and it works. You hierarchy should have an empty Mavhinegun game object with a machinegun script along with the muzzle_flash game object dropped into the variable.(You will have to make sure you drop the muzzle_flash into the empty game object Machinegun script so the muzzle_flash will work) But you don't have the actual machinegun yet so you can't drag anything yet. Just remeber to do it. Now grab your machine gun prefab from the weapons file in your project window. Drag it up and drop it into the empty machinegun to make it the child object of the empty Machinegun game object. The ACTUAL machinegun game object when you created it will have the muzzle_flash attached to it as a child of the machinegun. Now add another machinegunscript to the ACTUAL machinegun game object. Now add an "audio source"(componets/audio/audio source) to the ACTUAL machinegun game object. "UNMARK" the "Play on Awake" button but keep the "LOOP" MARKED> This script will have a muzzle_flash variable slot which you are going to drag and drop your Sparks prefab into.(Remember it's the empty machinegun game object in your Hierarchy that controls the muzzle_flash not the actual machinegun.) Go to MIsc file folder in your Project view and find the sparks prefab. NOW! Add an audio source to the Sparks prefab. And this time you will leave the "Play On Awake" MARKED! MARK the LOOP also! Now drag the Sparks prefab with the audio source into- Guess? This took me awhile to figure out. Drag it into the muzzle_flash variable in the actual machinegun game object script. So what you should have in your Hierarchy is two machinegun scripts and and two audio sources. The two machinegun scripts will each have a muzzle_flash variable. The top will have the muzzle_flash and the lower one will have the Sparks. The two audio sources will have the "Play on Awake" UNMARKED in the ACTUAL machinegun and the MARKED on the muzzle_flash variable(Sparks

more ▼

answered Jan 23 '11 at 11:10 PM

trimtro gravatar image

trimtro
1 4 4 4

(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:

x1031
x527
x21
x19

asked: Jan 23 '11 at 04:40 AM

Seen: 2777 times

Last Updated: Nov 10 '12 at 04:09 PM