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 user-5140 (google) · Oct 02, 2010 at 07:41 AM · soundloopwalksprint

Play Looped Sound?

I am making a simple First Person game and im trying to make it when im holding W, a footstep sound repeats.

Im only a beginner in Coding so i need some help, This is what i got but im not sure how to make it loop

Thanks,

var StepSound : AudioClip;

function Update() {

if (Input.GetKeyDown("w")) { audio.PlayOneShot(StepSound); } } @script RequireComponent(AudioSource)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by devilkkw · Oct 02, 2010 at 10:00 PM

else you have to select your sound in folder view,when u select see the inspector box,and chek the "loop". then use this script:

var StepSound : AudioClip;

function Update() {

if (Input.GetKey (KeyCode.W)) {

audio.clip = StepSound; audio.Play();

        } 

}

if u don't found the chekbox for loop,just use this script:

var StepSound : AudioClip;

function Update() {

if (Input.GetKey (KeyCode.W)) {

audio.loop = true; audio.clip = StepSound; audio.Play();

        } 

}

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
avatar image
1

Answer by Jesse Anders · Oct 02, 2010 at 01:08 PM

GetKeyDown() returns true during the update that the specified key was pressed; as such, that code will only play the footstep sound when the player begins moving forward (the sound won't loop).

GetKey() returns true during any update in which the specified key is held down. However, if you replace GetKeyDown() with GetKey(), you probably still won't get the expected results, since you'll be restarting the sound every update.

Options include starting the sound with looped playback when the key is pressed and stopping it when the key is released (although you'll probably need to add some extra logic to accommodate multiple keys), using a timer of some sort, and/or triggering the footstep sounds based on motion rather than input events. (There are lots of threads in the forums on creating 'footstep' sound effects, so if you haven't already, you might search there.)

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
avatar image
0

Answer by Bampf · Oct 02, 2010 at 03:30 PM

Instead of PlayOneShot, try assigning StepSound (your audio clip) to be the Audio Clip of your GameObject's AudioSource.) (Easily done in the inspector, unless your object plays more than one sound.)

audio.Play();

Also, look at the audio source in the inspector and make sure you've checked the Loop checkbox. (Or, set audio.loop = true just before you call Play).

So that will get the sound to loop. The remaining problem is starting and stopping it at the right time. You could can start on GetKeyDown like you are doing, on GetKeyUp call:

audio.Stop();

One more thing: I haven't tried this, but if the stopping sounds too abrupt, you might be able to set

audio.loop = false;

on key up instead, so that it will stop the sound after it's finished playing, rather than cutting it off. If you do that you would set loop = true just before you start playing it again.

Have a look at AudioSource documentation for other useful methods.

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 Jesse Anders · Oct 02, 2010 at 05:50 PM 1
Share

Did you mean 'audio.' rather than 'StepSound.' in your examples?

avatar image Bampf · Oct 02, 2010 at 09:26 PM 0
Share

Whoops, thought StepSound was an audio source, not an audio clip. Will attempt to correct. Thanks

avatar image
0

Answer by ryanconway · Apr 17, 2013 at 03:50 PM

Here is a nice simple way of doing it.

var StepSound : AudioClip; function Update() { if (Input.GetKeyDown (KeyCode.W)) { audio.loop = true; audio.clip = StepSound; audio.Play(); } else if (Input.GetKeyUp (KeyCode.W)) { audio.Stop(); } }

Just make sure your loop box is checked for the audio source, and their is appropriate silence at the end of the loop depending on the speed of running :)

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

1 Person is following this question.

avatar image

Related Questions

A node in a childnode? 1 Answer

How to obtain a gameObject from an List with the name? 1 Answer

How to use option setting Vibrate and Mute 1 Answer

Wwise Integration in Unity? 1 Answer

Sprinting Audio Problem 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