Footsteps and Bulletholes based on Material UVs?

Hello, I’m stumped with this one.
I’m trying to figure out how to access the Unity PBR material of a surface based on a raycast, and check whether or not it’s shared material is in one of multiple lists that determine what footstep sound effect to play, what bullet hole texture to use, whether one surface makes more sound when you walk across it (thief: dark project stealth mechanic) and so many other interesting things.

I’ve been looking around Unity Answers, and I believe this can also be done with a CharacterController collision check? Could someone give me an example? It sounds familiar.

If I understand correctly, you are trying to find the physics material of a gameobject; correct? Have you tried:

GetComponent<Collider>().sharedMaterial;

?

If you meant the shader material, try this:

Material[] mats = GetComponent<MeshRenderer>().sharedMaterials;
// now you have all materials assigned to this object; do what you want with them

or this if you only have one for each object:

Material mat = GetComponent<MeshRenderer>().sharedMaterial;

When you Raycast you can get a RaycastHit structure back on a hit. (Let’s call that variable raycastHit) In that will be a variable called “collider”. Now, either you have your meshes on this object or they are on some other object because you have a simplified collider.

If the collider is the same as the rendered mesh, then you can simply do: raycastHit.collider.GetComponent<Renderer>().sharedMaterial. If not, you will need to have a script that links the collider to the rendered mesh. (Call that ColliderToRendererLinker) You then would raycastHit.collider.GetComponent<ColliderToRendererLinker>().linkedRenderer.sharedMaterial.

Is that enough to guide you?