Getting ground texture at player position

Hi!

I’m trying to implement footstep sounds to my game and I want to define a bunch of textures and an array of audioclips to play when standing on these textures.

My code so far looks like this:

void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (!m_CharacterController.isGrounded)
            return;

        // Get texture at player coordinates
        RaycastHit groundHit;
        Texture2D textureAtCoord;

        if(Physics.Raycast(transform.position, Vector3.down, out groundHit))
        {
            textureAtCoord = (Texture2D)groundHit.transform.GetComponent<Renderer>().material.mainTexture;
            Debug.Log("Texture: " + textureAtCoord);

            // Find texture in TextureFootstep class
           

        }
    }


}

[System.Serializable]
public class TextureFootstep
{
    public string textureName;
    public Texture2D[] texture;
    public AudioClip[] clips;
}

Getting the texture on the ground at the players position is done. Now… how can I check if that texture is defined in my TextureFootstep class; and if it is - play a random audioclip corresponding to the defined texture?

public TextureFootstep[] footstepTextures;

you could go with a dictionary… key is the texture, value is the audioclip (or a struct of the data you want to attach to the texture), then you just pass in the texture you found to get the audio clip

I think I somehow got it… Do you mean I should create a Dictionary<Texture2D, AudioClip>? Can I use multiple audioclips for one key in a dictionary, like so: Dictionary<Texture2D, AudioClip[ ]>?

I think so… don’t think I’ve ever personally tried an array as a dictionary value though :slight_smile: