Simple Player Audio Management Script
Apr 28, 2021 18:41:37 GMT
komposite, Aaron, and 2 more like this
Post by eldv on Apr 28, 2021 18:41:37 GMT
Hello Invectorites!
I created this Player Audio Management script a while ago, and I thought I would share it. The Play Sound by State script is great, but more complex audio triggering can get a little unwieldy in the animator. This script once attached to your player can contain multiple audio clips for things like Attacking, Jumping, Breathing after Sprinting, Dying, etc. Some are set to play randomly while others are set to play by their index ID in their array. The functions can be called from the UnityEvents or AnimatorEvents during the animation directly. I hope this one can prove useful to other people in this community.
I created this Player Audio Management script a while ago, and I thought I would share it. The Play Sound by State script is great, but more complex audio triggering can get a little unwieldy in the animator. This script once attached to your player can contain multiple audio clips for things like Attacking, Jumping, Breathing after Sprinting, Dying, etc. Some are set to play randomly while others are set to play by their index ID in their array. The functions can be called from the UnityEvents or AnimatorEvents during the animation directly. I hope this one can prove useful to other people in this community.
using System.Collections;
using UnityEngine;
public class PlayerSFX : MonoBehaviour
{
[Header("The SFX played when Player dies:")]
public AudioClip[] deathFX = new AudioClip[5];
[Header("The SFX played when Player is injured:")]
public AudioClip[] hurtFX = new AudioClip[7];
[Header("The SFX played when Player is Sprinting:")]
public AudioClip sprintFX;
[Header("The number of breaths to play after Sprinting:")]
public int breaths;
[Header("The SFX played when Player is Attacking:")]
public AudioClip[] attackFX = new AudioClip[5];
[Header("The SFX played when Jumping:")]
public AudioClip[] jumpFX = new AudioClip[4];
[Header("The SFX played when exerting Effort:")]
public AudioClip[] effortFX = new AudioClip[4];
[Header("The SFX played when performing Misc Actions:")]
public AudioClip[] audioFX = new AudioClip[12];
[Header("The Volume control values for various SFX")]
public float vol;
public float attackvol;
public float breathvol;
public bool loop;
public void PlayDeathSound()
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.clip = deathFX[Random.Range(0, deathFX.Length)];
audiosource.volume = attackvol;
audiosource.Play();
}
public void PlayHurtSound()
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.clip = hurtFX[Random.Range(0, hurtFX.Length)];
audiosource.volume = attackvol;
audiosource.Play();
}
public void PlaySprintBreath()
{
StartCoroutine(Breathe());
}
IEnumerator Breathe()
{
for (int i = 0; i < breaths; i++)
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.clip = sprintFX;
audiosource.volume = breathvol;
audiosource.Play();
yield return new WaitForSeconds(sprintFX.length);
}
}
public void PlayAttackAudio(int id)
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.clip = attackFX[id];
audiosource.volume = attackvol;
audiosource.Play();
}
public void PlayJumpAudio(int id)
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.clip = jumpFX[id];
audiosource.volume = vol;
audiosource.Play();
}
public void StopAudio()
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.Stop();
}
public void PlayPushAudio()
{
StartCoroutine(PushAudio());
}
public void EndPush(int id)
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.Stop();
audiosource.clip = effortFX[id];
audiosource.volume = vol;
audiosource.Play();
}
public void PlayMiscAudio(int id)
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.Stop();
if (loop == true)
{ audiosource.loop = true; }
else { audiosource.loop = false; }
audiosource.clip = audioFX[id];
audiosource.volume = vol;
audiosource.Play();
}
public void AudioStop()
{
AudioSource audiosource = gameObject.GetComponentInParent<AudioSource>();
audiosource.Stop();
}
public IEnumerator PushAudio()
{
AudioSource audiosource = gameObject.GetComponent<AudioSource>();
audiosource.clip = effortFX[Random.Range(0, effortFX.Length - 1)];
audiosource.volume = vol;
audiosource.Play();
yield return new WaitForSeconds(audiosource.clip.length);
}
}