Snippets

Jesse Williams SFB Audio Controller Converted to C# (Unity)

Created by Jesse Williams

File AudioController_SFB_C.cs Added

  • Ignore whitespace
  • Hide word diff
+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;
+	}
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.