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

169 lines
5.1 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]
2024-04-08 18:57:32 +02:00
private float _speed = 0.5f;
[SerializeField]
private float _rotateSpeed = 180.0f;
[SerializeField]
private float _attackRange = 1.3f;
[SerializeField]
2024-04-08 21:31:29 +02:00
private float _jumpForce;
[SerializeField]
2024-04-08 18:57:32 +02:00
private float _idleThresholdFactor = 0.2f;
[SerializeField]
private float _noiseDelay = 5.0f;
[SerializeField, ShowOnly]
private float _noiseTimer;
2024-04-08 21:31:29 +02:00
[SerializeField]
private LayerMask _groundLayer;
private Rigidbody _rb;
private AudioSource _audioSource;
private bool _fadeOutNoise = false;
2024-04-08 18:57:32 +02:00
private Animator _animator;
private bool _isAttacking = false;
private float _idleThreshold;
2024-04-08 21:31:29 +02:00
private Vector3 _initialPosition;
2024-04-08 18:57:32 +02:00
[ContextMenu("Kill Zombie")]
private void TestDIe()
{
Die();
}
void Start()
{
_rb = GetComponent<Rigidbody>();
_noiseTimer = Random.Range(0.3f * _noiseDelay, 1.5f * _noiseDelay);
_audioSource = GetComponent<AudioSource>();
2024-04-08 18:57:32 +02:00
_animator = GetComponent<Animator>();
_idleThreshold = _speed / _idleThresholdFactor;
2024-04-08 21:31:29 +02:00
_initialPosition = transform.position;
}
2024-04-08 18:57:32 +02:00
void Update()
{
2024-04-08 18:57:32 +02:00
//_idleThreshold = _speed / _idleThresholdFactor;
UpdateNoise();
2024-04-08 18:57:32 +02:00
if (!_isAttacking)
{
MoveTowardsPlayer();
}
}
private void MoveTowardsPlayer()
{
2024-04-08 18:57:32 +02:00
Vector3 direction = (GameManager.Instance.Player.transform.position - transform.position);
Quaternion goalRotation = Quaternion.LookRotation(direction);
2024-04-08 21:31:29 +02:00
transform.rotation = Quaternion.Euler(0, Quaternion.RotateTowards(transform.rotation, goalRotation, _rotateSpeed * Time.deltaTime).eulerAngles.y, 0);
2024-04-08 18:57:32 +02:00
float diff = direction.magnitude;
2024-04-08 21:31:29 +02:00
//Debug.Log(diff);
2024-04-08 18:57:32 +02:00
if (diff <= _attackRange && !_isAttacking)
{
_animator.SetFloat("Running", 0);
_animator.SetBool("Idle", false);
StartCoroutine(Attack());
}
else
{
Vector3 oldPosition = _rb.position;
2024-04-08 21:31:29 +02:00
Vector3 goalDirection = new Vector3(direction.x, 0.2f, direction.z);
if (Physics.Raycast(transform.position + new Vector3(0, 0.1f, 0), goalDirection, 1f, _groundLayer))
{
Debug.Log("RayCast HIT");
//_rb.AddForce(Vector3.up * _jumpForce, ForceMode.Impulse);
_rb.MovePosition(_rb.position - transform.forward * Time.deltaTime);
_rb.MovePosition(_rb.position + new Vector3(0, _jumpForce, 0) * Time.deltaTime);
}
_rb.MovePosition(_rb.position + goalDirection * _speed * Time.deltaTime);
2024-04-08 18:57:32 +02:00
float dist = Vector3.Distance(oldPosition, _rb.position);
2024-04-08 21:31:29 +02:00
//Debug.Log($"Distance: {dist}, Threshold: {_idleThreshold * Time.deltaTime}");
2024-04-08 18:57:32 +02:00
if (dist > _idleThreshold * Time.deltaTime)
{
_animator.SetBool("Idle", false);
2024-04-08 21:31:29 +02:00
_animator.SetFloat("Running", _speed * 3.0f);
2024-04-08 18:57:32 +02:00
}
else
{
_animator.SetFloat("Running", 0);
_animator.SetBool("Idle", true);
}
}
}
private IEnumerator Attack()
{
_isAttacking = true;
_animator.SetBool("Attack", true);
2024-04-08 21:31:29 +02:00
yield return new WaitForSeconds(0.5f);
2024-04-08 18:57:32 +02:00
_animator.SetBool("Attack", false);
// check if actually hit
Vector3 direction = (GameManager.Instance.Player.transform.position - transform.position);
float diff = direction.magnitude;
2024-04-08 21:31:29 +02:00
if (diff <= _attackRange + 0.6f)
2024-04-08 18:57:32 +02:00
{
GameManager.Instance.Player.GetComponent<Character>().PickupItem(null);
}
_isAttacking = false;
}
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();
}
}
2024-04-08 18:57:32 +02:00
private void Die()
{
GameObject particleEffect = Instantiate(GameManager.Instance.ZombieDeathByDisableParticleEffect, transform.position, Quaternion.Euler(-90, 0, 0), GameManager.Instance.transform);
AudioSource audio = particleEffect.GetComponent<AudioSource>();
audio.clip = GetComponentInParent<ZombieSpawner>().ZombieDeathByDisableSoundeffects[Random.Range(0, GetComponentInParent<ZombieSpawner>().ZombieDeathByDisableSoundeffects.Count)];
audio.Play();
Destroy(gameObject);
}
public void FadeOutNoise()
{
_fadeOutNoise = true;
}
}