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
1
Question by jeremy · May 03, 2012 at 01:28 AM · audiosound

How to loop a sound clip on a ButtonDown function

Hi I need to know how to emit a sound clip when I press the up and down arrow keys, well I found out how to do this, (the script im using to do this is below), but the audio clip im using is a short footstep sound effect so when I click the up or down arrow keys it plays a OneShot function and I know thats because I wrote PlayOneShot, but I need it to loop as long as im holding down one of the keys, and im new at scripting so I don't know how to do this. Please Help, The code below is in JavaScript. If you are going to write a new cod can you please write it in Java, but its ok if you don't. Thanks

var concretefootsteps1Sound : AudioClip;

function Update () { if(Input.GetButtonDown("Vertical")){

   audio.PlayOneShot(concretefootsteps1Sound);
   
 }

}

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
0

Answer by syclamoth · May 03, 2012 at 01:35 AM

Well, what do you expect 'PlayOneShot' to do?

In any case, I'm pretty sure you don't want your code in Java, since Unity doesn't actually understand that language. If anything, I can give you C# (since that language is syntactically very similar), but I assume you are actually talking about UnityScript. Please don't get them mixed up, it's very annoying.

You should learn C#, btw, because there are much better tutorials for it out there on the internet and moreover it's a language that people actually use (yes, I know Javascript is a very common language, but Unity uses a very specific variant of the language that is pretty much unique).

Change that code to:

 function Update()
 {
     if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f && !audio.isPlaying)
     {
         audio.clip = concretefootsteps1Sound;
         audio.Play();
     }
     if(Mathf.Abs(Input.GetAxis("Vertical")) <= 0.1f && audio.isPlaying)
     {
         audio.Stop();
     }
 }
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 dpk · Dec 06, 2012 at 03:41 AM 0
Share

Have you found good C# tutorials for Unity Audio? Unity's own documentation seems more focused on using UnityScript.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

help sound on collision when key down 2 Answers

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

Stop another sound from another script? 1 Answer

How to play Audio Once if enemy has enterd a trigger 1 Answer

Audio Scripting 1 Answer


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