Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 JacksonWillmott · Sep 14, 2011 at 11:44 PM · fpssoundreload

How to play a reload sound when "r" pressed and clip empty (Using Dastardly Banana's FPS Package)

I'm using Dastardly Banana's download, "FPS Package", and I want to edit it slightly.

Through mass amounts of trial and error (and there was a lot more of the latter...), I've got the gun to play "Gunshot_Pistol" (Audio Clip) when the left mouse button is pressed by doing the following:

 var Gunshot_Pistol : AudioSource;
 
 
 
 function Update () {
 
    if(Input.GetButtonDown("Fire1")){
 
       Gunshot_Pistol.Play();
 
 
 
    }
 
 }

However, this was purely luck as I Googled how to do it. I have absolutely no scripting knowledge at all, and none of it makes sense to me. I used to use Blender with Logic Bricks until constantly-dropping framerate became an issue, so I switched to Unity.

So, any help with the following would be greatly appreciated. I need to know EXACTLY:

1) How to tell Unity to play "Reloading.wav" (Audio Clip) when my gun reloads EITHER when I press R or when it runs out of bullets in the clip

2) Where to put the AudioSource

If you wouldn't mind, a step-by-step guide would be amazing. I'm really new to Unity and it's great, but I just want my game to work properly.

Thank you in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 15, 2011 at 01:07 AM

You could add the AudioSource component to your character, then define the audio clips using AudioClip variables. You should also control the ammo: if you have bullets, shot; if run out of bullets but still have clips, reload; if no more clips or bullets, play the click sound - something like this:

var GunShotPistol: AudioClip; // drag the Gunshot_Pistol sound here var Reload: AudioClip; // drag the Reload sound here var Click: AudioClip; // drag the empty gun click here var bullets: int = 6; // bullets in gun var clips: int = 3; // how many clips you have var bulletsClip: int = 6; // six bullets per clip

function Update () { if (Input.GetButtonDown("Fire1")){ // when pressing the fire button... if (bullets > 0){ // AND you have bullets... audio.PlayOneShot(GunShotPistol); // play the sound bullets -= 1; // and decrement bullet count } else { // but if the bullets are over audio.PlayOneShot(Click); // play the click sound } } if (Input.GetKeyDown("r") || bullets < 0){ // "r" pressed OR no bullets... if (clips > 0){ // AND you have clips audio.PlayOneShot(Reload); // play the reload sound bullets = bulletsClip; // and reload bullet count clips -= 1; // decrement clips } } } If you don't want the click sound, just remove the else {...} part.
You can modify all variables above in the Inspector: if you want the clip to have 10 bullets, for instance, just change it at the Inspector. If you modify the variable during the game, the modification will be lost when the game ends; if you modify before running the game, however, the Inspector will save the new value and - be aware! - use it every time after, even if you change the value in the script! - this feature has been driving many experienced programmers to madness, so remember that!

Comment
Add comment · 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 JacksonWillmott · Sep 15, 2011 at 05:53 PM 0
Share

Awesome, thank you! I'll test it out and get back to you. Thanks again for the quick reply (Y)

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Temporarily disable audio? 1 Answer

Time.deltaTime stops counting up, after the Player is destroyed 1 Answer

Complete AI Tutorial? 1 Answer

Gun sway when moving with script? 0 Answers


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