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 Alayna · Jun 28, 2012 at 05:16 PM · audiotagfootsteps

Footstep Script Not Working

Hello, I have been trying to combine some footstep scripts together to create one that will detect the surface the character is on and play some sounds. Im currently at this point.

 var walkSoundsWood : AudioClip[];
 var walkSoundsGrass : AudioClip[]; // fill this array with the sounds at the Inspector
 var audioStepLength = 0.3;
 var groundType = int;
 
 function Start(){
     var controller : CharacterController = GetComponent(CharacterController);
     while (true) {
         if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
          if (groundType == 0)
             audio.clip = walkSoundsWood[Random.Range(0, walkSoundsWood.length)];
             audio.Play();
             yield WaitForSeconds(audioStepLength);
         } else if(controller.isGrounded && controller.velocity.magnitude > 0.3) {
            if (groundType == 1)
             audio.clip = walkSoundsGrass[Random.Range(0, walkSoundsGrass.length)];
             audio.Play();
             yield WaitForSeconds(audioStepLength);
           }
          else{
           yield;
         }
     }
 }
 
 
 function OnTriggerStay(type : Collider){
     if (type.tag == "Wood"){
       groundType = 0;
       print("Wood");
     }
      else if (type.tag == "Grass"){
        groundType = 1;
        print("Grass");
   }
 }


Im constantly getting a compiler error "BCE0022: Cannot convert 'int' to 'System.Type'." Can someone please help me figure out how to fix this script, or if I'm doing it in a needlessly complicated way? Thanks in advance for any help!

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 Loius · Jun 28, 2012 at 06:13 PM 0
Share

You can double-click the error in the console to open it to the problem line in $$anonymous$$onoDevelop. What line is causing the issue?

2 Replies

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

Answer by AlucardJay · Jun 28, 2012 at 07:11 PM

I think the problem is here :

 var groundType = int;

this should read :

 var groundType : int;

And where you say :

 while (true)

this makes no sense, there is no boolean called true, so what is the 'true' you are checking for?

but I can see you possibly having problems with groundType not having a value during the start function where you are checking if it == 0 || 1 . And do you want to only run this at start?

Also at that part of the script, both conditionals in your while statement are the same :

 if (controller.isGrounded && controller.velocity.magnitude > 0.3) {

did you mean for one to be

 if (! ...? (not), 

or detect different controller.velocity.magnitude?

also it would be good practice to typecast audioStepLength also :

 var audioStepLength : float = 0.3;
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 Alayna · Jun 28, 2012 at 07:18 PM 0
Share

Oh wow, I can't believe I didn't notice the improper '=' to ':' until you said that. Everything works great now, thank you so much! I also fixed the other things you said just for good measure. Just shows how much you need an outsider's opinion sometimes hah :)

avatar image AlucardJay · Jun 28, 2012 at 07:22 PM 0
Share

no worries, I missed the = on first read too =]

avatar image
0

Answer by Alayna · Jun 28, 2012 at 09:16 PM

Just in case other people have the same problem that I had, since it can be really hard (atleast for me) to find a working footsteps script, I will post it here. This works fine for me when put on the First Person Controller. The audioStepLength determines the space between each sound, and you can input sounds by increasing the element size in each section. To work you must place colliders near the ground that are set to is trigger, and tagged with the respective tags. I don't take any credit for this since it was mostly fusing a bunch of scripts from the tutorials and from Unity Answers.

 var walkSoundsWood : AudioClip[];
 var walkSoundsGrass : AudioClip[]; // fill this array with the sounds at the Inspector
 var audioStepLength : int = 0.3;
 var groundType : int;
 var isPlayerWalking : boolean;
 
 function Update(){
     //Check to see if the player is walking by checking for input from the walking keys
     if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
        //if those keys are pressed, the player must be walking...
        isPlayerWalking = true;
     }
     else{
        //if those keys aren't pressesd, the player can't be walking.....
        isPlayerWalking = false;
     }
 }
 
 function Start(){
         while(true){
          if (isPlayerWalking == true && groundType == 1){
             audio.clip = walkSoundsWood[Random.Range(0, walkSoundsWood.length)];
             audio.Play();
             yield WaitForSeconds(audioStepLength);
     }
           else if ( isPlayerWalking == true && groundType == 2){
             audio.clip = walkSoundsGrass[Random.Range(0, walkSoundsGrass.length)];
             audio.Play();
             yield WaitForSeconds(audioStepLength);
    }
          else{
           yield;
         }
     }
 }
 
 
 function OnTriggerStay(type : Collider){
     if (type.tag == "Wood"){
       groundType = 1;
       print("Wood");
     }
      else if (type.tag == "Grass"){
        groundType = 2;
        print("Grass");
   }
 }
     

I hope that it helps someone! Just get rid of the prints at the bottom and it should work!

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Help with Simple Footsteps? 2 Answers

Footsteps Script for Running and Walking 3 Answers

Stop another sound from another script? 1 Answer

Get acess and switch to many audio source in a single Object HELP!! 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