Enemies Chase Player But Won't Attack

My enemies will chase me and stop chasing when I’m too far away, but they’ll never attack me! The animation doesn’t play, I don’t lose health, nothing. I’ve done some console debugging, and can confirm that both the fighter.CanAttack() and InAttackRange() conditions return true when they’re supposed to, and the if statement does trigger.
Here’s the code from my AIController.cs

using System;
using System.Collections;
using System.Collections.Generic;
using RPG.Combat;
using UnityEngine;

namespace RPG.Control {
    public class AIController : MonoBehaviour {
        [SerializeField] float chaseDistance = 5f;

        Fighter _fighter;
        GameObject _player;
        
        void Start() {
            _fighter = GetComponent<Fighter>();
            _player = GameObject.FindWithTag("Player");
        }

        void Update() {
            if (InAttackRange() && _fighter.CanAttack(_player)) {
                Debug.Log(gameObject.name + " is attacking!");
                _fighter.Attack(_player);
            } else {
                _fighter.Cancel();
            }
        }

        bool InAttackRange() {
            return Vector3.Distance(_player.transform.position, transform.position) < chaseDistance;
        }
    }
}

It looks like we might need to see Fighter.cs as well, as this is where the actual attacks happen.

Fighter.cs

using System;
using System.Collections;
using System.Collections.Generic;
using RPG.Core;
using UnityEngine;

using RPG.Movement;


namespace RPG.Combat {
    public class Fighter : MonoBehaviour, IAction {
        [SerializeField] float attackRange;
        [SerializeField] float timeBetweenAttacks = 1f;
        [SerializeField] float weaponDamage;
        
        Health _target;
        float _timeSinceLastAttack;
        
        private void Update() {
            _timeSinceLastAttack += Time.deltaTime;
            if (!_target) return;
            if (_target.HasDied) return;
            
            if (!GetIsInRange()) {
                GetComponent<Mover>().MoveTo(_target.transform.position);
            } else {
                GetComponent<Mover>().Cancel();
                AttackBehavior();
            }
        }
        void AttackBehavior() {
            if (_timeSinceLastAttack < timeBetweenAttacks) return;
            transform.LookAt(_target.transform);
            
            // This will trigger the Hit() event
            GetComponent<Animator>().ResetTrigger("stopAttack");
            GetComponent<Animator>().SetTrigger("attack");
            _timeSinceLastAttack = 0;
        }
        
        // Animation Event
        private void Hit() {
            if (!_target) return;
            _target.TakeDamage(weaponDamage);
        }
        
        bool GetIsInRange() {
            return Vector3.Distance(transform.position, _target.transform.position) < attackRange;
        }

        public void Attack(GameObject combatTarget) {
            GetComponent<ActionScheduler>().StartAction(this);
            _target = combatTarget.GetComponent<Health>();
        }

        public void Cancel() {
            _target = null;
            GetComponent<Animator>().ResetTrigger("attack");
			GetComponent<Animator>().SetTrigger("stopAttack");
        }

        public bool CanAttack(GameObject target) {
            if (!target) return false;
            Health targetHealthComponent = target.GetComponent<Health>();
            return targetHealthComponent && !targetHealthComponent.HasDied;
            
        }
    }
}

I also looked at some of the other issues with this part of the course, trying solutions such as changing the chaseDistance and replacing the animation controller, but none of those worked.

Found the issue! I forgot to change the attack range in my enemy prefab, so it was at the default 0, which isn’t exactly possible to get that close to the player :sweat_smile:

That’s exactly what I was going to suggest as I looked at your code. With no default value,it will be zero in the inspector. Good job spotting that!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms