How can I make a Mecanim enemy AI for an adventure game?

What I exactly want to do is that the enemy should follow the player and then play the attack animation. I wish to recreate enemy AI like this:
video on youtube

although it my seem a ‘write my code’ question, I must tell you, I tried making it (and failed :P) Anyways, can you please tell me what modifications can I make to my script such that the enemies behave like in the video [5:17] 1 onwards?


var animator : Animator; //stores the animator component
var target : Transform; //the enemy's target 
var moveSpeed = 0.3; //move speed 
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
var Attacking = false;
function Awake() { myTransform = transform; }
function Start() 
 target = GameObject.FindWithTag("Player").transform; //target the player
animator = GetComponent(Animator);
 function Update()
if (Attacking == false)
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 //move towards the player, with animation on mecanim
myTransform.position += myTransform.forward ;
 animator.SetBool("Slash", true);//play attack animation via mecanim

function OnTriggerEnter(sensor : Collider) 
if(sensor.Tag == "PlayerFound")
{Attacking = true;}
{Attacking = false;}

Thanking You,
Shubham P.

I’m not against writing code if people appear to be trying. I’ve had problems with this (AI can get very complicated) It isn’t perfect (still working on the same problem as you) but here is some raw code that will get you on your way with making the AI chase the player if the player is within a certain distance.

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

function Chasing () 
     if(Vector3.Distance(transform.position,Player.position) >= MinDist){
          transform.position += transform.forward*MoveSpeed*Time.deltaTime;
          if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
                 // AI is within attack distance so play attack animation