forked from mdotstrange/CustomBehaviorDesignerTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayRandomAudio.cs
44 lines (39 loc) · 1.47 KB
/
PlayRandomAudio.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
using UnityEngine;
namespace BehaviorDesigner.Runtime.Tasks.Basic.UnityAudioSource
{
[TaskCategory("Basic/AudioSource")]
[TaskDescription("Plays a random audioclip from an audioclip list. Also randomizes the pitch. Make min,max = 1 for no random pitch.")]
public class PlayRandomAudio: Action
{
public SharedGameObject AudioSourceGO;
public SharedAudioClipList audioClips;
public SharedFloat volume = 1f;
public SharedFloat randomPitchMin;
public SharedFloat randomPitchMax;
public override TaskStatus OnUpdate()
{
if(AudioSourceGO.Value != null)
{
GameObject go = AudioSourceGO.Value.gameObject;
int max = audioClips.Value.Count;
AudioSource audio = go.GetComponent<AudioSource>();
AudioClip daClip = audioClips.Value[Random.Range(0, max)];
audio.clip = daClip;
audio.pitch = Random.Range(randomPitchMin.Value, randomPitchMax.Value);
audio.Play();
return TaskStatus.Success;
}
else
{
return TaskStatus.Failure;
}
}
public override void OnReset()
{
gameObject = null;
randomPitchMin = 0.80f;
randomPitchMax = 1.20f;
volume = 1;
}
}
}