how to make an enemy

im asking how to make an advanced enemy ai so when i near to the enemy it chase me but if im far from the enemy it stop chasing me when it is touching me it attacks thx:)

You can do that by controlling action based on distance and sight.

Step 1: Getting Distance From Player

// Use the Vector 3 function
float distance = Vector3.Distance (playerPosition, EnemyPosition);

Step 2: Get line of sight

// Add to following function to the enemy
bool CanSeePlayer () {
  // get the look direction
 Vector3 _lookDir = playerPosition- enemyPosition;

  // Crete line of sight to the player using raycasts
  Raycasthit[] hits = RaycastAll ( enemyPosition, _lookDir, Mathf.infinite);

  // See if the first object hit is the player
  return hits[0].transform == playerTransform;

}

You can now see of the player is visible using the CanSeePlayer function.

Step 3: Return to normal when out of line of sight

It is best to start a timer and if the player is out of line of sight for that entire time then return to normal. but for this lets assume when out of line of sight they stop chasing.

if (!CanSeePlayer) {
  // Return to idle state

   // Stop from continuing function
  return;
}

Step 4: Do Action based on distance

if (distance > outOfRangeDistance) {
        // Stop following player and go to idle action
    } else {
       // Start to follow the player
    }
    
    // Now check if it is in attach distance
   // we do not add this to the rest because the enemy should not stop to attack
   // if you want to make him stop to attach then use elsif statement above
    if (distance < attackDistane) {
      // Attack the player
    }