x


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!

more ▼

asked Jun 28 '12 at 05:16 PM

Alayna gravatar image

Alayna
219 8 11 15

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

Jun 28 '12 at 06:13 PM Loius
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
more ▼

answered Jun 28 '12 at 07:11 PM

alucardj gravatar image

alucardj
13.8k 34 56 87

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 :)

Jun 28 '12 at 07:18 PM Alayna

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

Jun 28 '12 at 07:22 PM alucardj
(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 28 '12 at 09:16 PM

Alayna gravatar image

Alayna
219 8 11 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5078
x1952
x1027
x323
x55

asked: Jun 28 '12 at 05:16 PM

Seen: 988 times

Last Updated: Jun 28 '12 at 09:16 PM