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 Zannart · Sep 12, 2018 at 09:17 PM · audioaudiosourceshootinglooping

audio loop when I click the MouseButtonDown(0)

Hi guys was wondering how I could loop a shooting sound while I am holding the MouseButtonDown(0), I succeded in making it sounds one time but then there is no looping, could you help me? this is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ShootSound : MonoBehaviour {

 public AudioSource AudioSource;
 private AudioClip clip;
 public GameObject gunPrefab;

 void Update () {
     if(Input.GetMouseButtonDown(0))
     {
         AudioSource.loop = true;
         playShot();           
     }
 }
 void playShot()
 {
     AudioSource = gunPrefab.GetComponent<AudioSource>();
     clip = AudioSource.clip;
     AudioSource.PlayOneShot(clip);
 }

}

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
1

Answer by I5 · Sep 13, 2018 at 02:14 AM

try this...but note that you'd have to use a separate game object just for the AudioSource so that it doesn't disable other things besides the audio. Also, just a reminder, Input.GetMouseButtonDown(0), will NOT return true while the button i held down, it will only return true the first frame, i.e. only once in Update. I used GetMouseButton in the code below, you may also just want to use that call instead of using the whole example

Edit: forgot to mention some key settings. To use the code below, configure your AudioSource to play on wake and make sure loop is also checked.

 AudioSource anAudioSource;
         void Update() {
             if (Input.GetMouseButton(0)) {
                 anAudioSource.gameObject.SetActive(true);
             } else if (anAudioSource.gameObject.activeInHierarchy) {
                 anAudioSource.gameObject.SetActive(false);
             }
         }
Comment
Add comment · Show 5 · 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 Zannart · Sep 13, 2018 at 09:18 AM 0
Share

i see... thank you very much! However, what if I wanted to play this sound when the function of shooting (of another script) is active rather than the click of the mousebutton(0)? How could I do it? thank you very much for your patience

avatar image tormentoarmagedoom Zannart · Sep 13, 2018 at 09:26 AM 0
Share

You can simply make the mouse button activate a bool vriable with On$$anonymous$$OuseDown, and desactivate with On$$anonymous$$ouseUp.

In a Update or LAteUpdate check if that bool is active to enable looping, and desactivate the looping when is false.

avatar image Zannart tormentoarmagedoom · Sep 13, 2018 at 10:28 AM 0
Share

yes, but if I keep holding the mouse button while my weapon is reloading the sound goes on ringing and so it's a bit weird... how could I make it stop?

avatar image I5 Zannart · Sep 13, 2018 at 02:51 PM 1
Share

that would be a new/different requirement, requiring a new solution, and now you're talking about "state" management, which isn't that hard, but it's not trivial either.


A good piece of example code for managing character shooting can be found in the assetstore (search for Unity's free S$$anonymous$$lth project tutoril).


I would provide code examples but it would likely result in a very long thread of piece-meal requirements gathering. That is, you should first try to identify all the things you want your characters and objects to be able to "do". Then before writing code, spend a few days trying to estimate the level of effort. You'll find that just adding a "reload" effect/feature, although cool and more realistic, will require a significant amount of effort (sound, animation synching, state management, etc), and if you don't have the resources/time, no problem, just get something from the assetstore, dirt cheap and will save you days, weeks and even months of coding. You may have do try different categories and search terms but you'll almost certainly find something to meet your needs.

avatar image Zannart I5 · Sep 13, 2018 at 04:29 PM 0
Share

ok, thank you a lot!

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

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio repeats 3 Answers

Stop audio looping on Input.GetButtonUp 0 Answers

What could make the audio loop? 2 Answers

Footsteps not playing on Flash Export 1 Answer

Play Audio on destroy 3 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