DugganSC

MeleeEnemy.cs

Apr 18th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TMPro;
  5. using UnityEngine;
  6.  
  7. public class MeleeEnemy : EnemyNavMeshAgent
  8. {
  9. [SerializeField] float _detectRange = 2f;
  10. [SerializeField] float _attackRange = 0.5f;
  11.  
  12. private void Start()
  13. {
  14. _animator = GetComponent<Animator>();
  15. }
  16.  
  17. protected override PlayerAttackable[] GetTargets()
  18. {
  19. Collider[] colliders = Physics.OverlapSphere(transform.position, _detectRange, LayerMask.GetMask(""));
  20. List<PlayerAttackable> playerAttackables = new List<PlayerAttackable>();
  21.  
  22. foreach (Collider collider in colliders)
  23. {
  24. if (collider.TryGetComponent<PlayerAttackable>(out PlayerAttackable attackable))
  25. {
  26. playerAttackables.Add(attackable);
  27. }
  28. }
  29.  
  30. return playerAttackables.ToArray();
  31. }
  32.  
  33. protected override void PerformAttack(PlayerAttackable nearestTarget)
  34. {
  35. _animator.SetBool("Punching", Vector3.Distance(transform.position, nearestTarget.transform.position) <= _attackRange);
  36. }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment