trigger sound by left mouse click ?

hello,
i’m trying to make a script in uJS (unity java script) when the you hold down the left mouse button a sound will play and once you let go of the left mouse button the sound will stop playing (the sound also needs to loop) but if you cant do the function of holding down left mouse could the function so hold button down.

Thank you for your time and help.

First you need to add a Audio Source component to a object. And in the inspector field of the Audio source, you add your audio clip and use set the option “Loop” to true.

After that, you write something like this, in the script for the object that has the Audio component:

void Update()
{ 
 // If the left mouse button is pressed down...
 if(Input.GetMouseButtonDown(0) == true)
 {
  GetComponent<AudioSource>().Play();
 } 
 // If the left mouse button is released...
 if(Input.GetMouseButtonUp(0) == true)
 {
  GetComponent<AudioSource>().Stop();
 }
}

This means, that when the left mouse button is pressed down, the Audio source file will start playing and it will loop as long as you have set the option to true. And since the GetMouseButtonDown only fires once when the mouse button is pressed down, it won’t start it over and over again.

GetButtonDown documentation.

And when you release the mouse button, the GetMouseButtonUp function will fire and stop your Audio source from playing. Just as with the GetMouseButtonDown, this will only fire once when you release the mouse button.

GetButtonUp documentation.

Good luck!