- Home /
What's the easiest way of changing footsteps sound depending on floor material?
Hi
I know basically nothing about coding and have so far just been taking scripts from various places in order to get my game to work. At the moment I have a script which plays random footstep sounds when I walk, which is fine, but I want to change these when the character is walking on certain materials. I have seen there seems to be several ways of doing this but I am a bit confused as to how I should go about doing it. My theory is that I would just make a box collider and then if the player is walking through it, it will play different sounds to when they aren't. I've got no idea how to do this however. If someone could just point me in the right direction I'm sure I could work it out. Thanks!
Answer by Peter G · Apr 24, 2010 at 02:22 PM
I believe the 2D platform tutorial uses triggers and tags
Two ways you could do it include:
1 Like you said, placing a large box collider marked as a trigger and tagged whatever material. Let's say "WoodFloor". Then your character would have a script with :
function OnTriggerStay (col : Collider) {
if (!col.tag) return;
var floorType : String = col.tag;
switch(floorType) {
case "WoodFloor":
// play wood floor footsteps.
break;
case "MetalFloor":
// play metal footsteps
break;
default: //if the tag is not a floor type
break;
}
}
2 Another very similar way would be to tag the floor and use OnControllerColliderHit or OnCollisionEnter to find the tag of the floor and play footsteps accordingly. The code is almost identical to the code above.
Hey, thanks for this I did manage to get it working. However, it seems to just play the audio I have on a constant loop and very quickly. I have been playing around for a couple of hours now with a few different codes but I've not managed to come up with anything, as I say my coding knowledge is pretty much nil. Basically I have the footsteps code from the fpstutorial, which works well. All I need to do is to say - if the player is in a box collider, play the sounds from var A, if not play them from var B, but I can't seem to work this out! :( Thanks for your help though
Answer by duck · Apr 24, 2010 at 04:21 PM
A good way is to create a sound with the same name as each material used as ground, then just detect the material's name of the ground under the player, and play the corresponding footstep sound based on its string name.
Hello, I was wondering if there are any examples of this? I have used something similar in a previous projet\ct so I am familiar with the concept, but i am unable to find any info on detecting material under the player (or on a specific poly collision etc.) Thanks so much for any help!
Answer by PeterDC · Oct 01, 2010 at 01:26 PM
In Unity 3 you can now use Audio Reverb Zones to easily change the way your footsteps sound.
Your answer
Follow this Question
Related Questions
AngryBots MaterialImpactManager Script and FootstepsAudio 1 Answer
Different Surface Footstep Sounds Whilst using W and E keys for footstep input 2 Answers
Switch Audio on Different surfaces 0 Answers
Varied footsteps - Object reference not set to an instance of an object? 2 Answers
Random footstep sounds not looping 1 Answer