Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created October 18, 2017 18:37
Show Gist options
  • Save unity3dcollege/0fa1a77a5b7cc5b24147c80805a259e6 to your computer and use it in GitHub Desktop.
Save unity3dcollege/0fa1a77a5b7cc5b24147c80805a259e6 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class FootSteps : MonoBehaviour
{
[SerializeField]
private AudioClip[] stoneClips;
[SerializeField]
private AudioClip[] mudClips;
[SerializeField]
private AudioClip[] grassClips;
private AudioSource audioSource;
private TerrainDetector terrainDetector;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
terrainDetector = new TerrainDetector();
}
private void Step()
{
AudioClip clip = GetRandomClip();
audioSource.PlayOneShot(clip);
}
private AudioClip GetRandomClip()
{
int terrainTextureIndex = terrainDetector.GetActiveTerrainTextureIdx(transform.position);
switch(terrainTextureIndex)
{
case 0:
return stoneClips[UnityEngine.Random.Range(0, stoneClips.Length)];
case 1:
return mudClips[UnityEngine.Random.Range(0, mudClips.Length)];
case 2:
default:
return grassClips[UnityEngine.Random.Range(0, grassClips.Length)];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment