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.

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
	          }
       }
    }
}

watch this tutorial… there’s a script in the description that will help you :slight_smile: