GameVsJam/3d Prototyp/Assets/Scripts/Zombie.cs

80 lines
1.7 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using Utility;
public class Zombie : MonoBehaviour
{
[SerializeField]
List<AudioClip> Noises;
[SerializeField]
public float speed = 5f;
[SerializeField]
private float _noiseDelay = 5.0f;
[SerializeField, ShowOnly]
private float _noiseTimer;
private Rigidbody _rb;
private AudioSource _audioSource;
private bool _fadeOutNoise = false;
void Start()
{
_rb = GetComponent<Rigidbody>();
_noiseTimer = Random.Range(0.3f * _noiseDelay, 1.5f * _noiseDelay);
_audioSource = GetComponent<AudioSource>();
}
void Update()
{
UpdateNoise();
MoveTowardsPlayer();
}
private void MoveTowardsPlayer()
{
Vector3 direction = (GameManager.Instance.Player.transform.position - transform.position).normalized;
_rb.MovePosition(_rb.position + direction * speed * Time.deltaTime);
}
private void UpdateNoise()
{
if (!_fadeOutNoise)
{
_noiseTimer -= Time.deltaTime;
if (_noiseTimer < 0)
{
_noiseTimer = _noiseDelay;
PlayRandomNoise();
}
}
else
{
_audioSource.volume -= _audioSource.volume * Time.deltaTime;
}
}
private void PlayRandomNoise()
{
if (!_audioSource.isPlaying)
{
_audioSource.clip = Noises[Random.Range(0, Noises.Count)];
_audioSource.Play();
}
}
public void FadeOutNoise()
{
_fadeOutNoise = true;
}
}