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;