Help me to finish my enemy ai script i just want to make a enemy patrolling when it's not detecting the player(JUST ADD THE SCRIPT IN MY JAVASCRIPT THANKS)

var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;

function Start ()


function Update ()

 if(Vector3.Distance(transform.position,Player.position) >= MinDist){
      transform.position += transform.forward*MoveSpeed*Time.deltaTime;

      if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
             print("ICAN SEE YU!!")




Have a look at Behaviour Tree, it’s a great tool to make such AI, in a simpler way.

I’m the author of Panda BT (, which is a script-based Behaviour Tree engine. Though it is not based on JavaScript but on c#.

The package contains an examples of the patrol-chase-attack behaviour among others AIs.

You are welcome on this thread if you have any question.