You've already forked zumbi-game
add random sfx to zombies
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Animator))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class ZombieController : MonoBehaviour
|
||||
{
|
||||
private static readonly int DieHash = Animator.StringToHash("Die");
|
||||
@@ -25,8 +25,14 @@ public class ZombieController : MonoBehaviour
|
||||
private readonly float attackCooldown = 1f;
|
||||
private float lastAttackTime;
|
||||
|
||||
[Header("Sounds")]
|
||||
[SerializeField] private AudioClip[] sfx;
|
||||
[SerializeField] private int sfxDelay = 5; // seconds
|
||||
|
||||
private Transform player;
|
||||
private Animator animator;
|
||||
private AudioSource audioSource;
|
||||
private float lastSfxPlay;
|
||||
|
||||
private bool IsDead() => currentHealth == 0;
|
||||
|
||||
@@ -35,12 +41,15 @@ public class ZombieController : MonoBehaviour
|
||||
currentHealth = maxHealth;
|
||||
animator = GetComponent<Animator>();
|
||||
player = GameObject.FindGameObjectWithTag("Player").transform;
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (IsDead()) return;
|
||||
|
||||
PlaySfx();
|
||||
|
||||
float distance = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
if (distance <= attackDistance)
|
||||
@@ -64,6 +73,23 @@ public class ZombieController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private AudioClip PickSfx()
|
||||
{
|
||||
return sfx[UnityEngine.Random.Range(0, sfx.Length)];
|
||||
}
|
||||
|
||||
private void PlaySfx()
|
||||
{
|
||||
if (Time.time - lastSfxPlay <= sfxDelay)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var clip = PickSfx();
|
||||
audioSource.PlayOneShot(clip);
|
||||
lastSfxPlay = Time.time;
|
||||
}
|
||||
|
||||
private void MoveToPlayer(float speed)
|
||||
{
|
||||
Vector3 direction = (player.position - transform.position).normalized;
|
||||
|
||||
Reference in New Issue
Block a user