- Home /
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.
Answer by CodeMasterMike · Jan 23, 2013 at 06:23 AM
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.
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.
Good luck!
This doesn't seem to work in unity 5, is there an updated answer or something? Could it be I am making a mistake somewhere I just tried to add this into a script and test it and I never heard anything. I also have an audio source attached to the player with LOOP checked.
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveCube : $$anonymous$$onoBehaviour
{
public float speed = 6f;
public AudioSource someSound;
void Start()
{
}
void Update()
{
// If the left mouse button is pressed down...
if (Input.Get$$anonymous$$ouseButtonDown(0) == true)
{
GetComponent<AudioSource>().Play();
}
// If the left mouse button is released...
if (Input.Get$$anonymous$$ouseButtonUp(0) == true)
{
GetComponent<AudioSource>().Stop();
}
if (Input.Get$$anonymous$$ey("w"))
{
transform.Translate(0, 0, 2 * speed * Time.deltaTime);
}
if (Input.Get$$anonymous$$ey("a"))
{
transform.Translate(-1 * speed * Time.deltaTime, 0, 0);
}
if (Input.Get$$anonymous$$ey("s"))
{
transform.Translate(0, 0, -1 * speed * Time.deltaTime);
}
if (Input.Get$$anonymous$$ey("d"))
{
transform.Translate(1 * speed * Time.deltaTime, 0, 0);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
someSound.Play();
}
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
transform.GetComponent<Animation>().Play("TornadoJump");
}
}
}
Your answer
Follow this Question
Related Questions
How to disable depth of field if Player moves Mouse X or Mouse Y for 2 seconds on trigger enter? 0 Answers
Alternate between two Audio clips on collision 3 Answers
Play sound on trigger, sound is coming from the trigger 1 Answer
Object Sound Trigger 1 Answer
Playing sound on collide with a trigger. 2 Answers