Adding Audioclips
Playing Basic Audio Clips
[SerializeField] private AudioClipSettings pushSound;
[SerializeField] private AudioEventPlayer audioPlayer;
audioPlayer?.PlayClip(pushSound);
AudioEventPlayer Explained
using UnityEngine;
namespace GrabMaster
{
[RequireComponent(typeof(AudioSource))]
public class AudioEventPlayer : MonoBehaviour
{
private AudioSource source;
void Awake()
{
source = GetComponent<AudioSource>();
}
// 1) Original API stays exactly the same:
public void PlayClip(AudioClipSettings settings)
{
if (settings?.clip == null || source == null) return;
PlayInternal(settings, 1f);
}
// 2) New overload for scaled volume (When louder sound based on impact speed:
public void PlayClip(AudioClipSettings settings, float volumeScale)
{
if (settings?.clip == null || source == null) return;
PlayInternal(settings, Mathf.Clamp01(volumeScale));
}
// private helper so you don’t duplicate pitch-and-play logic
private void PlayInternal(AudioClipSettings settings, float finalScale)
{
float randomPitch = settings.pitch * Random.Range(settings.randomPitchRange.x, settings.randomPitchRange.y);
source.pitch = randomPitch;
source.PlayOneShot(settings.clip, settings.volume * finalScale);
}
}
}
Last updated