Enemies will give chase no matter how far away the player is from them?

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

    private void Update()
    {
        if (DistanceToPlayer() < chaseDistance)
        {
            print(gameObject.name + "Should chase");
        }
    }

    private float DistanceToPlayer()
    {
        GameObject player = GameObject.FindWithTag("Player");
        return Vector3.Distance(player.transform.position, transform.position);
    }
}

}

this is my AIController, unsure why this isn’t working as it is in the lecture. Any suggestions?

Hmmm… the only thing I can think of is that your chaseDistance may be serialized at a higher value in the inspector of the enemy. If it is, that will override the 1f value you have in chasedistance.

It’s the same across both, thats what confused me the most. I also printed out the distance between the player and the enemy to the console. However when the player moves away from the enemy the distance is just 0 no matter where the player moves to.

  • Make sure the tag on the Enemy isn’t “Player” (The Player tag is like Highlander, ‘there can be only one’)
  • Make sure the AI Controller is on the Enemy, not the Player

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