- Home /
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);
}
}
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);
}
}
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
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.
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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