61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace _Project.Scripts.PlayerUtils
|
|
{
|
|
[RequireComponent(typeof(AudioSource))]
|
|
public class PlayerSound : MonoBehaviour
|
|
{
|
|
[Header("Настройки AudioSource")]
|
|
[SerializeField] private AudioSource audioSource;
|
|
|
|
[Header("Звуки Передвижения")]
|
|
[SerializeField] private AudioClip[] footstepSounds;
|
|
[SerializeField] private AudioClip jumpSound;
|
|
[SerializeField] private AudioClip landSound;
|
|
[SerializeField] private AudioClip slideSound;
|
|
|
|
[Header("Звуки Оружия")]
|
|
[SerializeField] private AudioClip shootSound;
|
|
|
|
private float _baseFootstepPitch = 1f;
|
|
|
|
private void Reset()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
}
|
|
|
|
public void SetFootstepPitch(float pitch) => _baseFootstepPitch = pitch;
|
|
|
|
public void PlayFootstep(bool isSprinting)
|
|
{
|
|
if (footstepSounds == null || footstepSounds.Length == 0) return;
|
|
|
|
int randomIndex = Random.Range(0, footstepSounds.Length);
|
|
audioSource.pitch = _baseFootstepPitch * Random.Range(0.92f, 1.08f);
|
|
|
|
float volume = isSprinting ? 0.6f : 0.4f;
|
|
audioSource.PlayOneShot(footstepSounds[randomIndex], volume);
|
|
audioSource.pitch = 1f;
|
|
}
|
|
|
|
public void PlayJump()
|
|
{
|
|
if (jumpSound != null) audioSource.PlayOneShot(jumpSound, 0.7f);
|
|
}
|
|
|
|
public void PlayLand()
|
|
{
|
|
if (landSound != null) audioSource.PlayOneShot(landSound, 0.5f);
|
|
}
|
|
|
|
public void PlaySlide()
|
|
{
|
|
if (slideSound != null) audioSource.PlayOneShot(slideSound, 0.7f);
|
|
}
|
|
|
|
public void PlayShoot()
|
|
{
|
|
if (shootSound != null) audioSource.PlayOneShot(shootSound, 0.8f);
|
|
}
|
|
}
|
|
} |