footsteps

Hi!

What is the best way to check which material (or even better: texture) the player is walking on? I was thinking about a raycast downwards, but how can I return information about its material or texture?

This makes it possible to trigger a sound like grass or rocks (footstep sounds).

Thank you.

You could add a tag to the object or, if you use tags for other things, create a list of tags for each sound or material.

The problem is that one single mesh may contain several materials (e.g a metal platform with sand texture on the right, resulting in two different footstep sounds), so tags wouldn’t be sufficient.

Divide the mesh in a grid and associate every cell to an specific sound…

If the mesh is too detailed… i will be a problem so a more sofisticated solution will be needed, like triggers for area colliders or use coded textures in the RGBA channels to get the appropiate sound (like the grid but in the texture and the color will be your sound index).

PD: Post a screen shot of your terrain…

.ORG

If you can combine the textures into one single texture, then there is an easy way to find which surface you are walking on. The RaycastHit class contains a property for the UV coordinate of the hit point. Map out your texture image so that each type of surface (sand, metal, etc) is contained in a specific rectangular area of the texture image. Then you just need to see which rectangle the hit point’s UV falls within and you will know which surface is underfoot.

if (Physics.Raycast(transform.position, Vector3.up*-1, hit, 5)) {
	var hitMaterial = hit.collider.renderer.sharedMaterial;
	print ("Material: " + hitMaterial.name + "   Texture: " + hitMaterial.mainTexture.name);
}

Though there might be other issues, as other people have mentioned, depending on what you’re doing exactly.

–Eric

Thanks for all the tips guys! Wonderful.
Eric5h5; thanks for the code!

:smile:

if (Physics.Raycast(transform.position, Vector3.up*-1, hit, 5)) {
	var hitMaterial = hit.collider.renderer.material;
	print ("Material: " + hitMaterial.name + "   Texture: " + hitMaterial.mainTexture.name);
}

For making this really perfect, you want to use sharedMaterial in this case, because that will not instantiate the material so that it is unique for modification, since you are only reading the value.

Oops, right…I knew that. :wink: Fixed code above…

–Eric

that’s only going to get the main texture though. so you could only have 2 sounds (by a bool) - right? say you want 3 because you have dirt, grass and cement in a layered shader. painting vertex colors and checking those could work. you could have 4 sounds (rgba, one for each channel - kind of like what omar was getting at). more i suppose if you checked for mixed colors.

though most likely i’d take the easy route and build some simple, non-rendered geometry with an OnTriggerStay script to play the clip.

It would have as many sounds as you want. It depends on each type of terrain being a different object with its own material though. But if you’re not using blended textures, that seems like the easiest solution.

–Eric