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 Jammer3000 · Mar 11, 2013 at 03:46 PM · audiokeyboard

How to play loop an audio clip on button hold down?

Hi I have this code(code below) that is working, but I don't just want it to play the audio once then stop. I need it so that when I hold down the left, right, up or down arrow keys it will loop the my audio clip which is about 1 to 2 seconds long, but stop when the arrow keys are not being pressed. It's my characters walking sound by the way and the audio clip I'm trying to play is a footstep sound. Also Please do it in java. Thanks

Here's the code:

var sound : AudioClip;

function Update () { if(Input.GetButtonDown("Fire1")) { audio.PlayOneShot(sound); audio.Play(); } }

Comment
Add comment · Show 1
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 ByteSheep · Mar 11, 2013 at 03:50 PM 0
Share

change GetButtonDown to GetButton

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ByteSheep · Mar 11, 2013 at 05:04 PM

Just add a simple check to see if the audio is already playing:

 var sound : AudioClip;
 
 function Update () { 

   if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) 
   { 
     if(!audio.isPlaying)
     {
       audio.PlayOneShot(sound); 
       audio.Play(); 
     }
   } 

 }


Here's an alternative that lets you control how much of the audio clip you want to play before looping:

 var sound : AudioClip;
 var timer : float = 100.0;
 var clipLength : float;
 
 function Start() {
   //clip the end of the audio file by 0.2 sec
   clipLength = sound.clip.length - 0.2;
 }
     
 function Update () { 
 
   if(audio.isPlaying)
   {
     timer += Time.deltaTime;
   }
 
   if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) 
   {
      if(timer >= clipLength)
      {
         audio.PlayOneShot(sound); 
         audio.Play(); 
         timer = 0.0;
      }
   } 
 
 }
Comment
Add comment · Show 12 · 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 Jammer3000 · Mar 11, 2013 at 06:22 PM 0
Share

That worked merry_christmas it does exactly what I wanted, but it just waits to long before it plays it again and I want it to play on w,s,d,a.

avatar image ByteSheep · Mar 11, 2013 at 06:34 PM 1
Share

I edited it to play when the arrow keys or w,a,s,d keys are pressed..
The audio file is looping, so if you want it to be shorter you will have to shorten the audio file.
Or it could also be done quite easily through code if you'd like an example.
You can mark my answer as accepted by clicking the green tick if it has answered your question so others will know the question has been solved..

avatar image Jammer3000 · Mar 12, 2013 at 12:20 PM 0
Share

Thank you so much merry_christmas for all your help it works great! Thanks BlackWIngsCorp your answer worked to, it just didn't do what I wanted, but thanks for the help(:

avatar image Jammer3000 · Mar 12, 2013 at 12:25 PM 0
Share

By the way if it's not to much trouble could you convert this to C# for me? If not thats ok you have already done more than enough.

avatar image ByteSheep · Mar 13, 2013 at 05:42 PM 0
Share

Well having someone else convert it for you won't help you learn C#. The conversion is easy enough, just remember the following:

• in C# functions need to be declared with the type of data they will return, e.g. function Start() would be void Start()

• javascript variables are declared like var VariableName : VariableType; and in C# like this VariableType VariableName;

e.g. var testvar : boolean; would become boolean testvar;

Those are the main things you will have to keep in $$anonymous$$d! If you still have problems converting it then please post back with what you have managed to convert so far.

Show more comments
avatar image
0

Answer by BlackWingsCorp · Mar 11, 2013 at 03:50 PM

Try audio.loop(sound);

Comment
Add comment · Show 2 · 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 Jammer3000 · Mar 11, 2013 at 05:00 PM 0
Share

Thanks the audio.loop(sound); just gives me the error below and merry christmas's answer made the sound play while I'm holding down the button, but it plays to fast, I need it to fully play the clip before it starts playing it again and nobody really did what I asked. The reason I gave so much information is so that someone can right a code example, I am not very good at coding yet.

avatar image BlackWingsCorp · Mar 11, 2013 at 05:46 PM 0
Share

How much time does the sound take before it ends? Let's say 3 secs. Put something like this: PS: watch out for the brackets and tell me how it works

  vat timer : float = 4
     if(Input.GetButtonDown("The Button")){
     if(timer == 0){
     audio.PlayOneShot(sound);
     timer -= Time.deltaTime;
     }
     if(timer == 0){
     timer = 4;
     }
     } 
avatar image
0

Answer by saik007 · Jun 17, 2016 at 03:57 PM

void Update () { if (Input.GetKeyDown (KeyCode.UpArrow)) {

         sample.Play ();

     } else if (Input.GetKeyUp (KeyCode.UpArrow)) {
         sample.Stop ();
     }
Comment
Add comment · 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

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

13 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

Related Questions

How to play several audio cllips one after another. 3 Answers

How to play an audio one shot clip on a collision 1 Answer

Why my sound it's not attaching to my Flashlight 2 Answers

Stop another sound from another script? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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