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 Blink · Aug 07, 2012 at 10:38 AM · soundfootsteps

Footsteps?

I have wrote this script hoping that it would play my footstep sound when w or s is held down but I have a lode of errors, is there any easier way to do this or am I nearly there?

var footsteps : AudioClip; var waitTime = 0.5;

function Update () {

if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){ yield WaitForSeconds(waitTime); audio.Play(footsteps);

} }

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 Fattie · Aug 07, 2012 at 11:54 AM 0
Share

If you are a beginner, never, ever ever use anything like yield, waitfor, or coroutines. (for that matter, there are very few, probably No, situations where you should use or even know about Update.)

In any event, never ever use yield, etc, if you are at this level.

Invoke and InvokeRepeating are the basic commands used in making video games.

You will need to learn how to use them and you should do that now. They are extremely simple.

4 Replies

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

Answer by aldonaletto · Aug 07, 2012 at 11:19 AM

You can't use yield inside Update or any other periodic routine (LateUpdate, FixedUpdate, OnGUI), but a very similar approach can work if started at Start (yes, Start can use yield!):

var footsteps : AudioClip; var waitTime = 0.5;

function Start () { // create an infinite loop that runs every frame: while (true){ if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){ yield WaitForSeconds(waitTime); audio.Play(footsteps); } yield; // let Unity free till next frame } } This implements a kind of private Update: the code inside the while infinite loop is executed every frame, then the yield instruction lets Unity free to take care of its business until next frame.

Comment
Add comment · Show 7 · 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 Blink · Aug 07, 2012 at 11:28 AM 0
Share

Ive had this error since I started coding the sound it goes "No appropriate version of 'UnityEngine.AudioSource.Play' for the argument list '(UnityEngine.AudioClip)' was found." Its on line 9, whats going wrong here?

avatar image Seth-Bergman · Aug 07, 2012 at 11:36 AM 1
Share

Ah..

you can either switch to : audio.PlayOneShot(footsteps);

or

audio.clip = footsteps;

audio.Play();

avatar image aldonaletto · Aug 07, 2012 at 11:48 AM 0
Share

Oh, I have not noticed the wrong use of Play: weirdly, the Play argument is a delay expressed in number of samples, not the AudioClip reference or name - this is very unintuitive, and almost everybody makes this mistake when starting using Unity (me included...)

avatar image Blink · Aug 07, 2012 at 12:01 PM 0
Share

This works perfectly now thanks, just one more thing how could the script be changed so that it plays say 3 different footstep sounds randomly, so it doesn't get repetitive?

avatar image aldonaletto · Aug 07, 2012 at 12:09 PM 0
Share

Use an AudioClip array, populate it in the Inspector and select the sound with Random.Range:

var footsteps: AudioClip[]; // drag the sounds here var waitTime = 0.5;

function Start () { // create an infinite loop that runs every frame: while (true){ if ($$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.1){ yield WaitForSeconds(waitTime); audio.PlayOneShot(footsteps[Random.Range(0,footsteps.length)]); } yield; // let Unity free till next frame } }

Show more comments
avatar image
0

Answer by Seth-Bergman · Aug 07, 2012 at 11:18 AM

you can't use WaitForSeconds in Update, because it's called every frame, but otherwise this should work, just remove that line..

but if you really want the pause, you can create a coroutine to call from Update:

     var footsteps : AudioClip;
     var waitTime = 0.5;
 
     function Start(){
 audio.clip = footsteps;
 }
 
     function Update () {
     if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
     WaitFootsteps();
     else
     makeSound = false;
     if(makeSound)
      audio.Play();
     
     } 
     
     
     
     function WaitFootsteps(){
     if(!makeSound){
     yield WaitForSeconds(waitTime);
     makeSound=true;
     }
     
     }

this should pause before the FIRST step, I assume that's what you wanted.. If you're just trying to pause between EVERY step, just loop the clip and make it have a half-second pause in the actual audio clip, that would make more sense...

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 Fattie · Aug 07, 2012 at 12:08 PM 1
Share
 InvokeRepeating("sound", 0.5,0.5);
avatar image
0

Answer by UnrealIzzy · Aug 07, 2012 at 11:39 AM

This script will do the job!

     var Walk : AudioClip;
 
 function Update () {
     if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
 {
 if(!audio.isPlaying) // this line may not even be necessary
 audio.Play();
 audio.loop = true;
 }else
 audio.loop = false;
 }

When you press either w,a,s or d then it will play the sound you decide to play. Hope this helped :D

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 Seth-Bergman · Aug 07, 2012 at 11:45 AM 1
Share

this could be shortened considerably:

function Update () {

 if (Input.Get$$anonymous$$ey("w") || Input.Get$$anonymous$$ey("a") || Input.Get$$anonymous$$ey("s") || Input.Get$$anonymous$$ey("d"))
 {
 if(!audio.isPlaying) // this line may not even be necessary
 audio.Play();
 audio.loop = true;
 }else
 audio.loop = false;

}

avatar image UnrealIzzy · Aug 07, 2012 at 11:47 AM 0
Share

Yep, Good work :D

avatar image
0

Answer by DaveA · Feb 09, 2014 at 03:33 AM

Just grab this asset: http://u3d.as/content/qlc/advanced-footstep-system/6o2

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simple footsteps? 1 Answer

My Running sound doesn't play or sound as it should, Help 1 Answer

Walking sound Script returns with Error: BCE0077 2 Answers

Changing Footstep Sound When On Different Surface? 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