You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
79 lines
2.6 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using static Unity.Barracuda.TextureAsTensorData;
|
|
using static UnityEditor.PlayerSettings;
|
|
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
public static AudioManager instance { get; private set; }
|
|
|
|
public List<AudioObject> m_AudioObjects = new List<AudioObject>();
|
|
|
|
private void Awake()
|
|
{
|
|
if(instance != null && instance != this)
|
|
{
|
|
Destroy(this);
|
|
} else
|
|
{
|
|
instance = this;
|
|
}
|
|
}
|
|
|
|
public void FootStepAudio( Vector3 bp_Position, float magnitude, PhysicMaterial groundType)
|
|
{
|
|
AudioObject match = FindAudioObjectByType(groundType);
|
|
|
|
if (magnitude > match.m_VelcoityMin && match != null)
|
|
{
|
|
|
|
AudioClip randomPick = match.m_AudioClip[Random.Range(0, m_AudioObjects.Count - 1)];
|
|
// Play sound
|
|
if (randomPick != null)
|
|
{
|
|
float volume = Mathf.Lerp(0f, match.m_Volume, ( magnitude - match.m_VelcoityMin) / (match.m_VelcoityMax - match.m_VelcoityMin) );
|
|
CustomAudioOneShot(bp_Position, randomPick, volume, match.m_PitchMin, match.m_PitchMax);
|
|
}
|
|
}
|
|
}
|
|
|
|
private AudioObject FindAudioObjectByType(PhysicMaterial groundType)
|
|
{
|
|
// Check for PM match
|
|
foreach(AudioObject obj in m_AudioObjects)
|
|
{
|
|
if(obj.m_GroundType.name == groundType.name)
|
|
{
|
|
return obj;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void CustomAudioOneShot(Vector3 position, AudioClip clip, float volume, float pitchMin, float pitchMax)
|
|
{
|
|
|
|
GameObject audioObject = new GameObject("FootstepAudio");
|
|
audioObject.transform.position = position;
|
|
|
|
// Add an AudioSource component to the GameObject
|
|
AudioSource audioSource = audioObject.AddComponent<AudioSource>();
|
|
|
|
// Set the AudioClip to play
|
|
audioSource.clip = clip;
|
|
|
|
// Adjust sound properties
|
|
audioSource.volume = Mathf.Pow((volume), 2); // Adjust the volume (0.0f to 1.0f)
|
|
audioSource.pitch = Random.Range(pitchMin, pitchMax); // Randomize the pitch within a range (e.g., 0.9 to 1.1)
|
|
// You can adjust other properties here such as spatial blend, loop, etc.
|
|
|
|
// Play the sound
|
|
audioSource.Play();
|
|
|
|
// Clean up the GameObject after the sound finishes playing
|
|
Destroy(audioObject, clip.length);
|
|
}
|
|
}
|
|
|