Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by simpleone · Feb 26, 2011 at 10:34 PM · audiosoundbulletshootfire

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 ?

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
4
Best Answer

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();

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 metalmutha11 · Mar 31, 2012 at 02:56 PM 0
Share

i have tried this but still get just sound at the start and not when im shooting ?

avatar image
3

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);
 }


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

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.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Setting bullet instansiate direction? help? 1 Answer

Activate sound without Pro filters 0 Answers

Problem With Bullet Prefab 2 Answers

Change bullet direction with player direction 1 Answer

3D Audio Source plays choppy/poping in web player (Video) 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