• 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 Tim.Holman · Aug 12, 2011 at 07:56 PM · audiorandomfootstepspitch

Foot step audio- randomize pitch

I'm trying to play audio of footsteps whenever my character is moving. The Lerpz example will not work because we are in first person mode, so the player does not have two feet with colliders, just one capsule collider that glides along. Therefore, I just need a script that randomizes the pitch of our footstep audio clip, then plays it when the player is moving forward, backward, or side to side. I am not an experienced progammer, so please be kind!! Any help would be greatly appreciated.

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

2 Replies

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

Answer by aldonaletto · Aug 12, 2011 at 08:17 PM

The FPS Tutorial has a good example. If you've downloaded it already, look for FpsPlayer.js script - the tutorial Assets folder has the footstep sounds too. If you don't have this tutorial, use the script below. You should attach it to the player, then at the Inspector set the walkSounds array size to 5 (or the number of sounds you want) and define each element to a different footstep sound.

var walkSounds : AudioClip[]; // fill this array with the sounds at the Inspector var audioStepLength = 0.3;

function Start(){ var controller : CharacterController = GetComponent(CharacterController); while (true) { if (controller.isGrounded && controller.velocity.magnitude > 0.3) { audio.clip = walkSounds[Random.Range(0, walkSounds.length)]; audio.Play(); yield WaitForSeconds(audioStepLength); } else { yield; } } } But if you prefer to vary the pitch as you've suggested, you can use this:

var stepLenght = 0.28;

function Start(){ var controller : CharacterController = GetComponent(CharacterController); while (true) { var vel = controller.velocity.magnitude; if (controller.isGrounded && vel > 0.3) { audio.pitch = 0.9 + 0.2*Random.value; // randomize pitch in the range 1 +/- 0.1 audio.Play(); yield WaitForSeconds(stepLength*vel/0.3); // shorten the step length with speed } else { yield; } } }

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 Tim.Holman · Aug 12, 2011 at 09:46 PM 0
Share

This script works great... with one exception. For some reason, the audio does not stop looping when the player stops moving. From what I can tell in the script, it should (but I'm not a programmer, so what do I know!) How might I go about preventing the audio clips from playing when the player is not moving?

avatar image aldonaletto · Aug 12, 2011 at 10:09 PM 0
Share

It should stop whenever the character speed fell below 0.3.
Which script have you used, the first (array of sounds) or the second (varying pitch)?

avatar image Tim.Holman · Aug 12, 2011 at 10:14 PM 0
Share

I used the first script utilizing 4 differnt audio clips. As for the character, I'm using the basic first person controller from the standard assets.

avatar image aldonaletto · Aug 12, 2011 at 11:11 PM 0
Share

I tested it in a First Person Controller and it worked fine. The if right above the audio.clip... line only executes the audio instructions if the character is grounded AND its velocity is higher than 0.3, exactly to ensure that the sound will stop when the character slows down too much. Did you copy and paste exactly the same script? Or have you done some adaptation to your case?

avatar image Tim.Holman · Aug 15, 2011 at 02:31 PM 0
Share

Interesting... I'm using the first script at the top. I tried applying the same script to the same first person controller in a test scene, and it seemed to work fine. However, in our actual game scene, the footsteps continually loop after the player has stopped moving. Could this be caused by other audio sources attached to our first person controller that are set to loop? If not, I'm not quite sure where to look or what I need to do here... Thank you very much for your help by the way Aldonaletto!!

Show more comments
avatar image
0

Answer by lewchico · Aug 12, 2011 at 08:49 PM

Hi there..I have a similar issue to this....would like to vary a sound's pitch each time it is triggered....tried to modify your above script but does not compile...

function Start(){

         audio.clip = squeakysample[Random.Range(0, squeakysample.length)];
         audio.Play();
        
     }


Also...do I attach this to the sample itself?

Apologies for newbiness...just beginning unity recently...cheers!

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 aldonaletto · Aug 12, 2011 at 09:21 PM 0
Share

@lewchico, please post this question as a new question - I can't answer it here because it would mess things up in Unity Answers - people would think this question has 3 answers, nobody will be able to find your question, etc.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Random footsteps 5 Answers

Random Audioclip. No Repeat ?? 1 Answer

Random Footsteps within CollisionSoundEffects Script 1 Answer

Set audio.pitch limits 1 Answer

Footstep Script Not Working 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