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 RM_Cell · Jul 05, 2013 at 05:40 AM · audiowalkingfootsteps

Footstep Audio Not Working?

Hi, I'm pretty new to scripting and just using Unity in general, and I've actually been pretty fine until now. I'm trying to make a horror game, and as we all know, sounds are key to this genre. I'm trying to insert footsteps when the player is walking, but for some reason it's not working. I have the script set under the player, and then an Audio Source set under the player as well with the sound "ForestFootsteps.mp3" under Audio Clip. Here's what my script looks like:

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

And for some reason, it's just not working. It's probably something stupid on my part, cause as I said I'm pretty new haha. But if someone could tell me what I'm doing wrong, or maybe even introduce a better way to script this (I know I kept it pretty basic) that'd be great. Thanks in advance!

Comment
Add comment · Show 2
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 AlucardJay · Jul 05, 2013 at 07:22 AM 1
Share

It is working, but only for one frame.

  • http://docs.unity3d.com/Documentation/ScriptReference/Input.html

check what each of the following commands does :

  • http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$ey.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$eyUp.html

The same thing goes for GetButton and Get$$anonymous$$ouseButton.

You want Get$$anonymous$$ey. ( Description : Returns true while the user holds down the key identified by name. Think auto fire. )

The problem you'll have after this is the audio will start playing every frame that the key is held.

So you could use a boolean and Invoke to only play the sound if it is not playing :

 var audioFile : AudioClip;
 var isPlaying : boolean = false;
  
 function Update()
 {
     if ( Input.Get$$anonymous$$ey( $$anonymous$$eyCode.W ) )
     {
         if ( !isPlaying )
         {
             audio.clip = audioFile;
             audio.Play();
             isPlaying = true;
             Invoke( "HasStoppedPlaying", audioFile.Length );
         }
     }
     else
     {
         audio.clip = audioFile;
         audio.Stop();
         isPlaying = false;
         CancelInvoke( "HasStoppedPlaying" );
     }
 }
  
 function HasStoppedPlaying()
 {
     isPlaying = false;
 }

You should have a look at a script I wrote, or check out my videos :

  • http://answers.unity3d.com/questions/373508/footsteps-script-for-running-and-walking.html

  • http://www.youtube.com/watch?v=HLxqEvT4Z8k

  • http://www.youtube.com/watch?v=2$$anonymous$$Hzp6od9WA

avatar image create3dgames · Jul 05, 2013 at 10:12 PM 0
Share

Yeah. What alucardj said.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RaptureFace · Jul 20, 2014 at 03:12 PM

The script

 #pragma strict
  
 var walk : AudioClip;
 var run : AudioClip;
  
 var isWalking : boolean = false;
 var isRunning : boolean = false;
  
  
 function Update()
 {
 GetState();
 PlayAudio();
 }
  
  
 function GetState()
 {
 if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
 {
 if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
 {
 // Running
 isWalking = false;
 isRunning = true;
 }
 else
 {
 // Walking
 isWalking = true;
 isRunning = false;
 }
 }
 else
 {
 // Stopped
 isWalking = false;
 isRunning = false;
 }
 }
  
  
 function PlayAudio()
 {
 if ( isWalking )
 {
 if ( audio.clip != walk )
 {
 audio.Stop();
 audio.clip = walk;
 }
  
 if ( !audio.isPlaying )
 {
 audio.Play();
 }
 }
 else if ( isRunning )
 {
 if ( audio.clip != run )
 {
 audio.Stop();
 audio.clip = run;
 }
  
 if ( !audio.isPlaying )
 {
 audio.Play();
 }
 }
 else
 {
 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 AlucardJay · Jul 20, 2014 at 04:19 PM 0
Share

I did a modification of my script to include a sprint timer : http://answers.unity3d.com/questions/596645/limited-sprint.html

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

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

Related Questions

Footsteps Script for Running and Walking 3 Answers

Footstep Script Not Working 2 Answers

Play different sounds on different surfaces? 1 Answer

How do I determine which frame an animation is currently playing? 1 Answer

How do I stop footstep sound from playing when I release 'w'? 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