- Home /
Changing Footstep Sound When On Different Surface?
Okay. I have a script of footstep where if you walk, footstep sound will be heard. But I want to make it different when we go to different surface
I got 2 different surface in my game, cement and water. I already got footstep for cement but how to change footstep to water sound when we get in the water?
Here's the script of footstep
var Steps : AudioClip[];
private var isWalking : boolean = false;
private var controller : CharacterController;
function Awake()
{
controller = GetComponent(CharacterController);
}
function Update()
{
if(controller.velocity.sqrMagnitude > 0.15 )
{
isWalking = true;
}
else
{
isWalking = false;
}
}
InvokeRepeating("Walking", 1.0, 0.6);
function Walking()
{
if(isWalking)
{
AudioSource.PlayClipAtPoint(Steps[Random.Range(0,Steps.length)], gameObject.transform.position);
}
}
I got this script from tutorial on Youtube. I don't make this script. Do I need to change something in it? Or I need to make another script. I'm really noob at programming. I know the basics but not like this advance.
Yes you'll need to change some stuff. How are you going to tell if the player is walking on cement, water, ground, ice, whatever - do you already have that worked out?
This script is working for me. I don't think this script based on surfaces. This script just measure my movement and then make footstep sound be heard
I understand what it's doing currently, but you want it to do more such as play a different sound depending on what surface the player is walking on - - true?
Then I refer you back to my first comment - have you worked out how you are going to track the surface the player is walking on! Anyway, hope the vid linked by buckets gets you going.
Answer by Rasikh · Sep 16, 2013 at 06:50 AM
First apply a tag to surfaces. for concrete apply a tag of concrete and for water surfaces apply a tag of water.
then you can use raycast to detect on which surface you are right now. your Walking function will change as follows,
function Walking()
{
if(isWalking)
{
var hit : RaycastHit;
if(Physics.Raycast(transform.position, Vector3.down,hit ))
{
var floortag = hit.collider.gameObject.tag;
if (floortag == "Concrete")
{
//play concrete sound code
}
else if (floortag == "Water")
{
//play Water sound code
}
}
}
}
What about if it's a terrain and it got different textures ?
Answer by buckets · Sep 16, 2013 at 06:33 AM
watch this tutorial... there's a script in the description that will help you :)