Files

79 lines
2.4 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerNoise : MonoBehaviour
{
[Header("Радиус шума от шагов")]
public float sneakRadius = 3f;
public float walkRadius = 8f;
public float runRadius = 18f;
[Header("Настройки генерации")]
public float stepInterval = 0.5f;
private float stepTimer = 0f;
public GameObject wavePrefab;
[Header("Синхронизация")]
[Tooltip("Если включено и на объекте есть EchoTrigger, генерация шума шагов синхронизируется с анимацией персонажа через EchoTrigger")]
public bool syncWithEchoTrigger = true;
void Update()
{
if (syncWithEchoTrigger && GetComponent<EchoTrigger>() != null)
{
return;
}
if (Keyboard.current == null) return;
bool isMoving = Keyboard.current.wKey.isPressed ||
Keyboard.current.aKey.isPressed ||
Keyboard.current.sKey.isPressed ||
Keyboard.current.dKey.isPressed;
if (isMoving)
{
stepTimer -= Time.deltaTime;
if (stepTimer <= 0f)
{
EmitStepNoise();
stepTimer = stepInterval;
}
}
else
{
stepTimer = 0f;
}
}
public void EmitFootstep(Vector3 footPosition, float radius)
{
if (wavePrefab != null)
{
Instantiate(wavePrefab, footPosition, Quaternion.identity);
}
Collider[] colliders = Physics.OverlapSphere(footPosition, radius);
foreach (Collider hit in colliders)
{
BlindMutantAI mutant = hit.GetComponent<BlindMutantAI>();
if (mutant != null) mutant.HearSound(footPosition);
}
}
private void EmitStepNoise()
{
float currentRadius = walkRadius;
if (Keyboard.current != null)
{
if (Keyboard.current.leftShiftKey.isPressed) currentRadius = runRadius;
else if (Keyboard.current.leftCtrlKey.isPressed || Keyboard.current.cKey.isPressed) currentRadius = sneakRadius;
}
Vector3 footPosition = transform.position - new Vector3(0, transform.localScale.y / 2, 0);
EmitFootstep(footPosition, currentRadius);
}
}