Created
October 18, 2017 18:37
-
-
Save unity3dcollege/0fa1a77a5b7cc5b24147c80805a259e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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