///PROBLEM: The player should be damaged but it isn’t… i’ve tried to use different ways of damaging and they just dont work any help would be greatly appreciated ![]()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class MonsterAttackSystem : MonoBehaviour
{
[Header("CORE")]
[SerializeField] private Transform player = null;
private PlayerHealth PlayerHealthh = null;
[SerializeField] private float DamageOnAttack = 10f;
[SerializeField] private float AttackTime = 2f;
[SerializeField] private float AttackRange = 5f;
private float ATime;
[Header("ADDITIONAL")]
[SerializeField] private AudioClip AttackSFX = null;
[SerializeField] private AudioSource audioSource = null;
[Header("UNITY EVENTS")]
[SerializeField] private UnityEvent OnEnemyAttacked;
private void Awake()
{
PlayerHealthh = FindObjectOfType<PlayerHealth>();
}
private void Update()
{
if (Vector3.Distance(this.transform.position, player.position) < AttackRange && Time.time < ATime)
{
TryAttack();
ATime = Time.time + AttackTime;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, AttackRange);
}
private void TryAttack()
{
if(PlayerHealthh == null) { return; }
PlayerHealthh.TakePlayerHealthDamage(DamageOnAttack);
OnEnemyAttacked?.Invoke();
if(audioSource == null || AttackSFX == null) { return; }
audioSource.pitch = Random.Range(0.75f, 1.25f);
audioSource.PlayOneShot(AttackSFX, audioSource.volume);
}
}