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 MonsterGamingHD · Apr 01, 2013 at 02:39 AM · runsoundswalk

How to make different walk sounds

Hello there! I have a script where you can walk and run, and it will play a sound. But, I need a script where different surfaces or objects when walking upon them, different sounds begin. I dont want it to be said to me that you can just put different sounds next to the object. Here is my script:

 #pragma strict
 @script RequireComponent( AudioSource )
  
 var walk : AudioClip;
 var run : AudioClip;
  
 var walkAudioSpeed : float = 0.4;
 var runAudioSpeed : float = 0.2;
  
 private var walkAudioTimer : float = 0.0;
 private var runAudioTimer : float = 0.0;
  
 var isWalking : boolean = false;
 var isRunning : boolean = false;
  
 var walkSpeed: float = 7; // regular speed
 var runSpeed: float = 20; // run speed
  
 private var chCtrl: CharacterController;
 private var chMotor: CharacterMotor;
  
 function Start()
 {
 chCtrl = GetComponent(CharacterController);
 chMotor = GetComponent(CharacterMotor);
 }
  
 function Update()
 {
     SetSpeed();
  
     if ( chCtrl.isGrounded )
     {
        PlayFootsteps();
     }
     else
     {
        walkAudioTimer = 0.0;
        runAudioTimer = 0.0;
     }
 }
  
 function SetSpeed()
 {
     var speed = walkSpeed;
  
     if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
     {
        speed = runSpeed;
     }
  
     chMotor.movement.maxForwardSpeed = speed;
 }
  
 function PlayFootsteps()
 {
     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;
     }
  
     // Play Audio
     if ( isWalking )
     {
        if ( audio.clip != walk )
        {
          audio.Stop();
          audio.clip = walk;
        }
  
        //if ( !audio.isPlaying )
        if ( walkAudioTimer > walkAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          walkAudioTimer = 0.0;
        }
     }
     else if ( isRunning )
     {
        if ( audio.clip != run )
        {
          audio.Stop();
          audio.clip = run;
        }
  
        //if ( !audio.isPlaying )
        if ( runAudioTimer > runAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          runAudioTimer = 0.0;
        }
     }
     else
     {
        audio.Stop();
     }
  
     // increment timers
     walkAudioTimer += Time.deltaTime;
     runAudioTimer += Time.deltaTime;
 }
Comment
Add comment · Show 5
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 EliteMossy · Apr 01, 2013 at 02:45 AM 0
Share

You could use physic materials on the floors and play a sound based on the physic material that is being walked on. I did this to $$anonymous$$e.

avatar image MonsterGamingHD · Apr 01, 2013 at 02:51 AM 0
Share

How do I do that?

avatar image EliteMossy · Apr 01, 2013 at 03:05 AM 1
Share

Well, you just do a check in OnCollisionStay for what physic material the player is standing on, and play the appropriate sound. You could by all means just use tag's for each floor surface.

avatar image AlucardJay · Apr 01, 2013 at 06:13 AM 0
Share

Hello? This is my script remember? I have a script ... don't forget to give credit where credit is due. People will appreciate that you recognize their efforts, and decide to help you further if you actually show some gratitude.

What you are asking is quite technical, AAA title stuff.

Elite$$anonymous$$ossy has actually given you the 2 methods to accomplish this.

Use a trigger volume, and on entering that trigger swap to a different set of sounds.

Or read the information from the texture under which the character is standing, then use that information to choose what sound set you are playing from.

That is how you do it. What you are asking is for someone to write it for you, perhaps you should have done it on my forum page, (considering it's my script and all ...., I know exactly what it's doing)

Finally to put a positive spin on things, I will give away another method of at least randomizing the sounds.

Line 4 :

 var walk : AudioClip[];

Line 5 :

 var run : AudioClip[];

Line 85 :

 audio.clip = walk[ Random.Range(0, walk.Length) ];

Line 101 :

 audio.clip = run[ Random.Range(0, run.Length) ];

With this it will pick a random sound from the arrays of audio clips and play that each footstep, so at least every footstep doesn't sound exactly the same.

Not for different surfaces, but it makes this script better.

avatar image Chronos-L · Apr 02, 2013 at 09:18 AM 0
Share

@monsterga$$anonymous$$ghd, which part of these methods did you not understand? They are properly explained, and you can find lots of examples using Google with keywords like "check physic material", "gameObject tag", "trigger", "assign variable in another script"

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by MonsterGamingHD · Apr 02, 2013 at 05:46 AM

Make a guide tutorial on it. I don't really understand . I understand word better

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 EliteMossy · Apr 02, 2013 at 08:52 AM 0
Share

You should not respond with an Answer if it is not an Answer.

avatar image
0

Answer by EliteMossy · Apr 02, 2013 at 09:17 AM

OK, based on your code, i came up with a quick 2 minute solution, it should work but not guaranteed because i don't use UnityScript, but here goes.

Simply tag the different floor surfaces, i.e ConcreteFloor, WoodFloor, etc. Then you can easily add more types of floors in.

 #pragma strict
 @script RequireComponent( AudioSource )
  
 var defaultFloorWalk : AudioClip;
 var defaultFloorRun : AudioClip;
 
 var concreteFloorWalk : AudioClip;
 var concreteFloorRun : AudioClip;
  
 var woodFloorWalk : AudioClip;
 var woodFloorRun : AudioClip;
  
 var walkAudioSpeed : float = 0.4;
 var runAudioSpeed : float = 0.2;
  
 private var walkAudioTimer : float = 0.0;
 private var runAudioTimer : float = 0.0;
  
 var isWalking : boolean = false;
 var isRunning : boolean = false;
  
 var walkSpeed: float = 7; // regular speed
 var runSpeed: float = 20; // run speed
  
 private var chCtrl: CharacterController;
 private var chMotor: CharacterMotor;
  
 function Start()
 {
 chCtrl = GetComponent(CharacterController);
 chMotor = GetComponent(CharacterMotor);
 }
  
 function Update()
 {
     if ( chCtrl.isGrounded )
     {
         if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
         {
             if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
            {
              // Running
                 isWalking = false;
                 isRunning = true;
                 chMotor.movement.maxForwardSpeed = runSpeed;
             }
             else
             {
                 // Walking
                 isWalking = true;
                 isRunning = false;
                 chMotor.movement.maxForwardSpeed = walkSpeed;
             }
         }
         else
         {
             // Stopped
             isWalking = false;
             isRunning = false;
         }
         // increment timers
         walkAudioTimer += Time.deltaTime;
         runAudioTimer += Time.deltaTime;
     }
     else
     {
        walkAudioTimer = 0.0;
        runAudioTimer = 0.0;
     }
 }
 
 function OnCollisionEnter(collisionInfo : Collision){
      PlayFootstepSound(collisionInfo.gameObject.tag);
 }
 
 function PlayWalkingSound(clip : AudioClip){
     if (audio.clip != clip){
         audio.Stop();
         audio.clip = clip;
     }
     if ( walkAudioTimer > walkAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          walkAudioTimer = 0.0;
        }
 }
 
 function PlayRunningSound(clip : AudioClip){
   if ( audio.clip != clip )
        {
          audio.Stop();
          audio.clip = clip;
        }
  
        if ( runAudioTimer > runAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          runAudioTimer = 0.0;
        }
 }
 
 function PlayFootstepSound(floortype : String)
 {    
     switch (floortype){
         case "ConcreteFloor":
             if (isWalking)
                 PlayWalkingSound(concreteFloorWalk);
             else
                 PlayRunningSound(concreteFloorRun);
             break;
         case "WoodFloor":
             if (isWalking)
                 PlayWalkingSound(woodFloorWalk);
             else
                 PlayRunningSound(woodFloorRun);
             break;
         default:
             if (isWalking)
                 PlayWalkingSound(defaultFloorWalk);
             else
                 PlayRunningSound(defaultFloorRun);
             break;
     }
 }

You can modify the switch statement to add more types of floor, and tag the floor appropriatly.

This is the best i could come up with in the few minutes spare time i had.

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

Multiple Cars not working 1 Answer

Expecting : found "=" 1 Answer

Lerpz ThirdPersonPlayerAnimation bug - running at walking speed 1 Answer

collider then change rotation converse? 0 Answers

Please help me to make a run, jump and fly through the air. 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