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 Irsan · Apr 14, 2011 at 06:13 AM · footstepskeysholdwasd

Play footstep sound loop while holding any of the WASD keys.

I want to play and loop a sound if I am holding down one of the WASD keys. How would I go about doing this?

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

6 Replies

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

Answer by Aurelio Provedo · Apr 14, 2011 at 06:40 AM

Add an AudioSource component to the object, and set it to looping in the editor. Then create a script and add it to the object too, after adding this code in the Update method:

if ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) )
        audio.Play();
    else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && audio.isPlaying )
        audio.Stop(); // or Pause()

I haven't actually checked it, but it should work. You should get the idea.

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 Aurelio Provedo · Apr 14, 2011 at 09:19 AM 1
Share

Just a small edit. The first condition would look better like this: if ( ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) ) && !audio.isPlaying ) This way the sound will not be played again when more than one key is pressed at a time.

avatar image
0
Best Answer

Answer by Nolirneen · Apr 14, 2011 at 06:33 AM

I've never tried putting in audio before, but perhaps you're looking for something like this?

var AudioFile : AudioClip;

function Update() {

 if (Input.GetKeyDown ("w"))
 {
     audio.clip = AudioFile;
     audio.Play();
 }

 else
 {
     audio.clip = AudioFile;
     audio.Pause();
 }

}

It should work. =)

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 sambomo100 · Jul 24, 2012 at 04:30 AM 0
Share

Nolirneen, in the script, where would to describe what sound it is? Like how would you tell the script what sound to play?

avatar image
1

Answer by cpt_me0w · Jul 25, 2013 at 06:04 PM

Hi, i know this is an old thread, but i still wanted to share. if you want to make the sound fade in and out depending on if the player is mooving or not, i actually made the audioclips volume dependant on the magnitude of the characters movement.

     audio.clip = playerWalkSound;
     audio.volume = horizontalMovement.magnitude * 0.01;
     
     if(horizontalMovement.magnitude > maxWalkSpeed * playerWalkSoundThreshholdRatio){
         if(!playerWalkSoundCreated){
             audio.Play();
             playerWalkSoundCreated = true;
         }
     }else{
     
         audio.Stop();
         playerWalkSoundCreated = false;
     }

i don't know if this is stupid or not, but i wanted to throw it out here :)

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 zero84085 · Sep 08, 2012 at 07:14 PM

be sure to check the (is grounded) topics because you dont want to press the jump button and the wasd keys and in the air, hear footsteps Here is: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController-isGrounded.html

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 Skyline Studios · Sep 16, 2012 at 10:14 PM

var AudioFile : AudioClip;

function Update() {

 if (Input.GetKeyDown ("w"))
 {
     audio.clip = AudioFile;
     audio.Play();
 }

 else

if (Input.GetKeyUp ("w")) { audio.clip = AudioFile; audio.Pause(); } }

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
  • 1
  • 2
  • ›

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sticky keys on all Unity apps 4 Answers

Locomotion System - difference between OnFootLand and OnFootStrike 1 Answer

Rigidbody problem 0 Answers

Footsteps script problem and question 0 Answers

Adding footsteps sounds to the Adventure tutorial 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