• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by trimtro · Jan 23, 2011 at 04:40 AM · audiosoundfiringmachinegun

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

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireDude67 · Jan 23, 2011 at 05:19 AM 1
Share

Look at the FPS Tutorial

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by DaveA · Jan 23, 2011 at 09:30 AM

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

Comment
Thom Denick

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by FLASHDENMARK · Jan 23, 2011 at 12:11 PM

var gunSound : AudioClip;

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

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Skyline Studios · Nov 10, 2012 at 04:09 PM 0
Share

how do you stop audio when mouse button is released

avatar image

Answer by trimtro · Jan 23, 2011 at 11:10 PM

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by GameCreater · Sep 13, 2011 at 05:28 AM

how do you do this for the mouse when clicked?

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Camando360 · Jan 19, 2012 at 02:47 AM 0
Share

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).

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sound responsive game mechanic 1 Answer

simple issue just need an expert/experienced advise AUDIO playback 1 Answer

AudioSource pitch measurement unit 1 Answer

UI Slider stays the same through multiple scenes. 0 Answers

Audio Needs to stop 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges