- Home /
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!
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?
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;
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 :)
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!
Your answer
Follow this Question
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