• 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
0
Question by NikoKNoob · Jul 01, 2015 at 02:01 AM · audioclip

Play Audio Sound On Left Click ("Fire1")

Hello, i am very new to Unity and have successfully made a first person slashing animation that triggers when i press ("Fire1") and moves along with a first person controller. Could somebody please give me instructions on how to attach an audio file, (called "Swoosh") to my character and give me a c# line of code for it to play when i press ("Fire1")

Thank you!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Jul 01, 2015 at 02:02 AM

I'll do you one better and point you to the Unity sound tutorials.

Audio Listeners and Sources

Here's the audio section of Unity's Space Shooter tutorial project. It explains in detail exactly what to do, and has sample code.

Comment
Add comment · Show 6 · 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 NikoKNoob · Jul 01, 2015 at 06:47 AM 0
Share

@Dave Carlie Thanks but I know all that stuff already, I want that video to show me what code I need to play it when I hit left click!

avatar image Dave-Carlile · Jul 01, 2015 at 11:39 AM 1
Share

I added another link to the Space Shooter tutorial that talks in detail how to do sound. I know you just want someone to give you the code (and multiple people have), but you need to go through the tutorial and learn how to do it. The space shooter tutorial explains exactly how to do it, including the exact code you're looking for.

avatar image NikoKNoob · Jul 01, 2015 at 04:14 PM 0
Share

Can you please give me the lik to the tutorial. I understanding where your co$$anonymous$$g from with the whole learning think, but when I look at those I don't know what the lines of code mean. If you could copy paste your whole comment explaining where the actual part where it triggers the sound when you press ("Fire1")

avatar image Dave-Carlile · Jul 01, 2015 at 04:18 PM 0
Share

You already have code that does something when the user presses Fire1 right? You're starting an animation? The same place you have that code, you call audio.Play().

If you look at the space shooter tutorial I linked above, scroll down and there is a bunch of code there. In the Update function it shows code that plays sound when the Fire1 button is pressed. It really doesn't get any more clear than that :)

avatar image NikoKNoob · Jul 01, 2015 at 10:26 PM 1
Share

I FIXED IT!!! Yay, thank you all who helped me along the way, even if I pissed you off ;)

Show more comments
avatar image
0

Answer by GiyomuGames · Jul 01, 2015 at 02:41 AM

To add to what Dave posted, if you want to play the sound at a specific time of your animation you can use an event: http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

Comment
Add comment · Show 3 · 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 NikoKNoob · Jul 01, 2015 at 06:41 AM 0
Share

I know it is possible, but I am very new and just need instructions, I'm pretty sure there is a code: Something like- if (Input.GetButtonDown("Fire1")-then what would I say after to play the specific audio clip

avatar image GiyomuGames · Jul 01, 2015 at 06:53 AM 0
Share

I think you'll find what you are looking for there: http://answers.unity3d.com/questions/182754/play-sound-on-button-press.html or there http://answers.unity3d.com/questions/12546/playing-audio-clip.html

avatar image NikoKNoob · Jul 01, 2015 at 07:21 AM 0
Share

Thank you but it is still very confusing for me, can somebody please just write the line of code, and on the bottem explain it, those other examples aren't clear enough. I just need it to play when I press ("Fire1")

avatar image
0

Answer by raja1250 · Jul 01, 2015 at 07:41 AM

attach an audio source to player,drag your file to audiosource then write this code under fire button press

gameObject.audio.Play();

Comment
Add comment · Show 2 · 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 NikoKNoob · Jul 01, 2015 at 07:43 AM 0
Share

Thank I'll try, do I write that under void update or start?

avatar image NikoKNoob · Jul 01, 2015 at 07:43 AM 0
Share

And so whenever I press ("Fire1") it will work?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unspecified Error during Audio Import 0 Answers

UnityWebRequestMultimedia to get AudioClip from API 2 Answers

Getting audioclips and video from localdrive? 0 Answers

audioSource.Play causes a 2 seconds hang 0 Answers

I got an error during the import of an audioclip. 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