-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepSoundController.cs
More file actions
77 lines (61 loc) · 1.96 KB
/
Copy pathStepSoundController.cs
File metadata and controls
77 lines (61 loc) · 1.96 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using UnityEngine;
using System.Collections;
public class StepSoundController : MonoBehaviour {
public bool m_singleton = false;
public static StepSoundController instance = null;
// If we're flagged as the singleton step manager, register ourself to the static variable
void Start () {
if( m_singleton )
{
instance = this;
}
}
// Keep track of how much time has passed since the last step
public float m_lastStep = 0f;
// An array of all of the different step sounds we can make
public AudioClip[] m_steps = new AudioClip[0];
// Movement speed is what drives how fast we make sounds. 1.0f is standard time
public float m_movementSpeed = 0f;
// How often we should play a step sound in seconds
public float m_stepRate = 0.0f;
private bool m_timeControlActive = false;
public AudioSource m_audioSrc = null;
// Update is called once per frame
void Update () {
if(m_movementSpeed <= 0f )
{
m_lastStep += Time.deltaTime;
return;
}
// Scale the amount of time that's passed since the last step based on the relative speed
m_lastStep += (Time.deltaTime * m_movementSpeed);
if( m_timeControlActive && m_stepRate > 0f && m_stepRate < m_lastStep )
{
m_lastStep = 0f;
Step();
}
}
// Called into to activate repeating sounds on a timer
// Does not reset the last step time, so can be called as often as necessary
public void SetTimeControlledSoundActive( bool enabled, float stepRate, float moveSpeed )
{
m_timeControlActive = enabled;
m_stepRate = stepRate;
m_movementSpeed = moveSpeed;
}
// The function "Step" can either be controller from an animation event or by way of timed interval
public void Step()
{
if( !m_audioSrc )
{
// Add an audio source component and reference it
AddComponent<AudioSource>();
m_audioSrc = GetComponent<AudioSource>();
}
if( m_audioSrc )
{
m_audioSrc.clip = m_steps[ Random.Range( 0, m_steps.Length ) ];
m_audioSrc.PlayOneShot( m_audioSrc.clip );
}
}
}