A way to create an invisible circle for the enemy?

Good evening everyone! I was wondering if there is a way to create a kind of invisible circle around my enemy cube so that when the player gets in, the enemy aggros him and moves and attacks him. But when the player leaves the circle area, the enemy should go on following him and attacking.These are the AI and Attack scripts i use for my enemy:

using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;

// Use this for initialization
void Start () {
	attackTimer = 0;
	coolDown = 2.0f;
}

// Update is called once per frame
void Update () {
	if(attackTimer > 0)
		attackTimer -= Time.deltaTime;
	
	if(attackTimer < 0)
		attackTimer = 0;
	if(attackTimer == 0){
		Attack();
		attackTimer = coolDown;
	}
		
	
}

private void Attack(){
	float distance = Vector3.Distance(target.transform.position, transform.position);
	
	Vector3 dir = (target.transform.position - transform.position).normalized;
	
	float direction = Vector3.Dot(dir, transform.forward);
	
	Debug.Log (distance);
	
	if(distance < 2.5f) {
		if(direction > 0) {
		PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
		eh.AdjustCurrentHealth(-10);
	}
}

}
}

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;

private Transform myTransform;

void Awake() {
	myTransform = transform;
}	

// Use this for initialization
void Start () {
	GameObject go = GameObject.FindGameObjectWithTag("Player");
	
	target = go.transform;
	
	maxDistance = 2;
}

// Update is called once per frame
void Update () {
	Debug.DrawLine(target.position, myTransform.position, Color.yellow);
	
	//Look at target
	myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
	
	if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
	//Move towards target
	myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;	
}

}
}

An easy way would be to check the distance between the center of the “enemy cube” and the player. The code below assumes you have your player name “Player” and your enemy cube namde “EnemyCube”. Change the names to fit the names in your game. So at the top of the file you would put:

public GameObject goPlayer;
public GameObject goEnemyCube;

In Start() you would put:

goPlayer = GameObject.Find("Player");
goEnemyCube = GameObject.Find("EnemyCube");

The in your code in update where you want to decide between two behaviors:

if (Vector3.Distance(goPlayer.transform.position, goEnemyCube.transform.distance) < circleRadius) {
  // Do whatever you want when an enemy is inside the circle
}

‘circleRadius’ will be the outside distance from the center of the enemy cube for the circle.

Ok so i wrote what you said as you can see:

using System.Collections;

public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
public GameObject goPlayer;
public GameObject goEnemy;

private Transform myTransform;

void Awake() {
	myTransform = transform;
}	

// Use this for initialization
void Start () {
	GameObject go = GameObject.FindGameObjectWithTag("Player");
	
	target = go.transform;
	
	goPlayer = GameObject.Find("Player");
	
	goEnemy = GameObject.Find("Enemy");
	
	maxDistance = 2;
}

// Update is called once per frame
void Update () {
	Debug.DrawLine(target.position, myTransform.position, Color.yellow);
	
	//Look at target
	myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
	
	if (Vector3.Distance(goPlayer.transform.position, goEnemy.transform.distance) < circleRadius) {
	// Do whatever you want when an enemy is inside the circle
	}
	if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
	//Move towards target
	myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;	
}

}
}

But i get these 3 errors: There is no definition for “distance” and 2 of them are for the Vector3. did i do something wrong? :S

A “kind of invisible circle,” you say… you mean, something like— this?

SphereCast is the thing which suits you best for this… for more info please follow the link -----> SphereCast Documentation

Don’t Forget to mark the answer if found useful… enjoy…Cheers…