Snippets
Created by
Jesse Williams
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class AudioClips
{
public string _name;
public AudioClip[] audioClips;
public float volume = 1.0f;
public bool volumeBySpeed = false;
public float minVolume = 0.2f;
public bool pitchBySpeed = false;
public float minPitch = 1.0f;
public float maxPitch = 1.0f;
}
public class AudioController_SFB_C : MonoBehaviour {
public AudioClips[] audioClipsArray; // Array of single use audio clips
public AudioClips[] audioLoopsArray; // Array of loops
public bool pitchByTimescale = true;
private float minLoopVolume = 0.2f; // Minimum volume for loop adjusted by speed
private float maxLoopVolume = 1.0f; // Maximum volume for loop adjusted by speed
private bool volumeBySpeed = false; // Should we adjust volume by speed?
private bool pitchBySpeed = false; // Should we adjust the pitch by speed?
private float pitchMin = 1.0f; // Desired min Pitch
private float pitchMax = 1.0f; // Desired max pitch
private float desiredPitch = 1.0f; // Desired Pitch
private float desiredMin = 0.0f; // The goal minimum volume
private float desiredMax = 1.0f; // The goal maximum volume
private float volumeMin = 0.0f; // The current minimum
private float volumeMax = 1.0f; // The current maximum
private float volumeSpeed = 1.0f; // Speed modifier for volume changes
private bool isWalking = false; // Are we walking?
private bool isFlying = false; // Are we flying?
AudioSource audioSource; // Default volume for current looped clip
Animator animator;
void Start ()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
}
void Update ()
{
UpdatePitch();
if (audioSource.isPlaying)
UpdateVolume();
CheckLoop();
}
void CheckLoop()
{
AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
if ((animatorStateInfo.IsName("Base Layer.Ground Locomotion")) && animator.GetFloat("locomotion") != 0f && !isWalking)
{
isWalking = true;
StartLoop("Walking Loop");
}
if (isWalking && (!animatorStateInfo.IsName("Base Layer.Ground Locomotion") || animator.GetFloat("locomotion") == 0.0f))
{
isWalking = false;
}
if ((animatorStateInfo.IsName("Base Layer.Air Locomotion") || animatorStateInfo.IsName("Base Layer.ground to air")) && !isFlying)
{
isFlying = true;
StartLoop("Flying Loop");
}
if (isFlying && (!animatorStateInfo.IsName("Base Layer.Air Locomotion")
&& !animatorStateInfo.IsName("Base Layer.ground to air")
&& !animatorStateInfo.IsName("Base Layer.fly dodge")
&& !animatorStateInfo.IsName("Base Layer.fly hit")))
{
isFlying = false;
}
if (!isWalking && !isFlying)
StopLoop();
}
void UpdateVolume()
{
if (volumeMin != desiredMin)
volumeMin = Mathf.MoveTowards(volumeMin, desiredMin, volumeSpeed * Time.deltaTime);
if (volumeMax != desiredMax)
volumeMax = Mathf.MoveTowards(volumeMax, desiredMax, volumeSpeed * Time.deltaTime);
audioSource.volume = volumeMax;
if (volumeBySpeed)
audioSource.volume = Mathf.Clamp(audioSource.volume * Mathf.Abs(animator.GetFloat("locomotion")), volumeMin, volumeMax);
if (audioSource.volume == 0.0f)
{
volumeBySpeed = false;
audioSource.Stop();
}
}
void UpdatePitch()
{
if (pitchBySpeed)
{
float pitchRangeNegative = 1.0f - pitchMin;
float pitchRangePositive = pitchMax - 1.0f;
float locomotion = animator.GetFloat("locomotion");
if (locomotion > 0.0f)
desiredPitch = 1.0f + (pitchRangePositive * locomotion);
else if (locomotion < 0.0f)
desiredPitch = 1.0f + (pitchRangeNegative * locomotion);
else
desiredPitch = 1.0f;
}
else
desiredPitch = 1.0f;
audioSource.pitch = Mathf.MoveTowards(audioSource.pitch, desiredPitch, Time.deltaTime);
if (pitchByTimescale)
audioSource.pitch = audioSource.pitch * Time.timeScale;
}
void PlayAudio(string name)
{
int id = AudioClipID(name);
float volume = audioClipsArray[id].volume;
AudioClip audioClip = audioClipsArray[id].audioClips[Random.Range(0,audioClipsArray[id].audioClips.Length)];
if (audioClipsArray[id].volumeBySpeed)
volume = Mathf.Clamp(volume * Mathf.Abs(animator.GetFloat("locomotion")), audioClipsArray[id].minVolume, audioClipsArray[id].volume);
AudioSource.PlayClipAtPoint(audioClip, transform.position, volume);
}
void StartLoop(string name)
{
int id = AudioLoopID(name);
desiredMin = audioLoopsArray[id].minVolume;
desiredMax = audioLoopsArray[id].volume;
minLoopVolume = audioLoopsArray[id].minVolume;
maxLoopVolume = audioLoopsArray[id].volume;
volumeBySpeed = audioLoopsArray[id].volumeBySpeed;
pitchBySpeed = audioLoopsArray[id].pitchBySpeed;
pitchMin = audioLoopsArray[id].minPitch;
pitchMax = audioLoopsArray[id].maxPitch;
AudioClip audioClip = audioLoopsArray[id].audioClips[Random.Range(0,audioLoopsArray[id].audioClips.Length)];
audioSource.clip = audioClip;
audioSource.Play();
}
void StopLoop()
{
desiredMin = 0.0f;
desiredMax = 0.0f;
}
int AudioClipID(string name)
{
for (int i = 0; i < audioClipsArray.Length; i++){
if (audioClipsArray[i]._name == name)
return i;
}
return -1;
}
int AudioLoopID(string name)
{
for (int i = 0; i < audioLoopsArray.Length; i++){
if (audioLoopsArray[i]._name == name)
return i;
}
return -1;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.