|
|
|
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 float m_FootStepMinVelocity = 5;
|
|
|
|
|
|
|
|
public float m_FootStepMaxVelocity = 40;
|
|
|
|
|
|
|
|
public AudioClip m_FootStep;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
if(instance != null && instance != this)
|
|
|
|
{
|
|
|
|
Destroy(this);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void FootStepAudio( Vector3 bp_Position, float magnitude, string groundType )
|
|
|
|
{
|
|
|
|
|
|
|
|
if (magnitude > m_FootStepMinVelocity)
|
|
|
|
{
|
|
|
|
// Play sound
|
|
|
|
if (m_FootStep != null)
|
|
|
|
{
|
|
|
|
float volume = Mathf.Lerp(0f, 1f, ( magnitude - m_FootStepMinVelocity) / (m_FootStepMaxVelocity - m_FootStepMinVelocity) );
|
|
|
|
//Debug.Log($"{magnitude}, {groundType}, {volume}");
|
|
|
|
CustomAudioOneShot(bp_Position, m_FootStep, volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CustomAudioOneShot(Vector3 position, AudioClip clip, float volume)
|
|
|
|
{
|
|
|
|
|
|
|
|
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(0.7f, 1.3f); // 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, m_FootStep.length);
|
|
|
|
}
|
|
|
|
}
|