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;
}
}
}