+using System.Collections;
+using System.Collections.Generic;
+ 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
+ audioSource = GetComponent<AudioSource>();
+ animator = GetComponent<Animator>();
+ if (audioSource.isPlaying)
+ AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
+ if ((animatorStateInfo.IsName("Base Layer.Ground Locomotion")) && animator.GetFloat("locomotion") != 0f && !isWalking)
+ StartLoop("Walking Loop");
+ if (isWalking && (!animatorStateInfo.IsName("Base Layer.Ground Locomotion") || animator.GetFloat("locomotion") == 0.0f))
+ if ((animatorStateInfo.IsName("Base Layer.Air Locomotion") || animatorStateInfo.IsName("Base Layer.ground to air")) && !isFlying)
+ 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")))
+ if (!isWalking && !isFlying)
+ 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;
+ audioSource.volume = Mathf.Clamp(audioSource.volume * Mathf.Abs(animator.GetFloat("locomotion")), volumeMin, volumeMax);
+ if (audioSource.volume == 0.0f)
+ float pitchRangeNegative = 1.0f - pitchMin;
+ float pitchRangePositive = pitchMax - 1.0f;
+ float locomotion = animator.GetFloat("locomotion");
+ desiredPitch = 1.0f + (pitchRangePositive * locomotion);
+ else if (locomotion < 0.0f)
+ desiredPitch = 1.0f + (pitchRangeNegative * locomotion);
+ audioSource.pitch = Mathf.MoveTowards(audioSource.pitch, desiredPitch, Time.deltaTime);
+ 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;
+ int AudioClipID(string name)
+ for (int i = 0; i < audioClipsArray.Length; i++){
+ if (audioClipsArray[i]._name == name)
+ int AudioLoopID(string name)
+ for (int i = 0; i < audioLoopsArray.Length; i++){
+ if (audioLoopsArray[i]._name == name)