-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPSFootSteps.cs
46 lines (38 loc) · 1.39 KB
/
FPSFootSteps.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
public class FPSFootSteps : MonoBehaviour {
[SerializeField]
private AudioClip[] grassSteps;
// Add more clips
private AudioSource audioSource;
private TerrainDetector terrainDetector;
private bool playClipCooldown = false;
private float walkStepInterval = 0.6f;
private float runStepInterval = 0.4f;
void Awake() {
audioSource = GetComponent<AudioSource>();
terrainDetector = new TerrainDetector();
}
public void Step(bool isRunning) {
if (!playClipCooldown) {
float stepInterval = walkStepInterval;
if (isRunning) {
stepInterval = runStepInterval;
}
playClipCooldown = true;
audioSource.PlayOneShot(GetStepClip());
// NOTE: Utilities.DoAfter is defined in: github.com/repl-games/Unity-DoAfter-CSharp
StartCoroutine(Utilities.DoAfter(stepInterval, ()=> playClipCooldown = false));
}
}
private AudioClip GetStepClip() {
// The order of the terrain will return in the order they show up in the
// layer pallete profile in the editor when you paint a texture on a terrain
int terrainTextureIndex = terrainDetector.GetActiveTerrainTextureIdx(transform.position);
switch(terrainTextureIndex) {
case 0: // grass
return grassSteps[UnityEngine.Random.Range(0, grassSteps.Length)];
default:
return grassSteps[UnityEngine.Random.Range(0, grassSteps.Length)];
}
}
}