Making An Enemy Follow Player In Range? C#

Hi I need my enemy to follow a player if in range. Here’s my script. Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyController : MonoBehaviour {

        public GameObject Player;    
      private GameObject wayPoint;
        private Vector3 wayPointPos;
        
        private float speed = 3.0f;
        void Start()
        {
            //At the start of the game, the zombies will find the gameobject called wayPoint.
            wayPoint = GameObject.Find("wayPoint");
        }
    
        void Update()
        {
            wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);

        //Here, the zombie's will follow the waypoint.

        transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
            }    
}

Hi i’m trying to make a script where my zombies chase ONLY if in range. Thanks if anyone can figure out.

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyController : MonoBehaviour {

        public GameObject Player;

    private GameObject wayPoint;
    private Vector3 wayPointPos;
    
    private float speed = 3.0f;
    void Start()
    {
        //At the start of the game, the zombies will find the gameobject called wayPoint.
        wayPoint = GameObject.Find("wayPoint");
    }

    void Update()
    {
        wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
        //Here, the zombie's will follow the waypoint.
        transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
            }
}

Good day.

Once you get the zombies to follow the player, you only need to know the distance between them to decide if shoot, move, stop, etc…

Check the distance between 2 positions with

float distance= Vector3.Distance (Object1.transform.position, Object2.transform.position);

Bye!