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
0
Question by soul1771 · Jan 22, 2013 at 05:08 PM · javascripttriggergamemousesound

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.

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

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.

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!

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 KUFgoddess · Nov 16, 2016 at 10:33 AM 0
Share

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

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

11 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

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


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