monster ai help needed

hi all,
i am making an ai script for my monsters,

in my script i have a attack routine, and a patroling routine . so it works like this, if player is in range , it attacks the player and if player is not in range it goes to do patroll its area, the problem is that my patroling is not working and i dont know whats wrong, it attacks the player just fine and chases as well but when i am out of the monsters range it does not patrol and when the game starts even though i am out of its range it doesnot patrol either. here is the code

using UnityEngine;

using System.Collections;

using System.Collections.Generic;





public class EnemyAI : MonoBehaviour

{

	public Transform target;

	public int moveSpeed;

	public int rotationSpeed;

	public int maxDistance;

	public int minDistance;

	public List<Transform> targets;

	public Transform selectedTarget;

	bool alive = true;

	private Transform myTransform;

	

	

	int random;

	//private Transform myTransform;

	

	void Awake () {

		myTransform = transform;

	}



	// Use this for initialization

	void Start ()

	{

		GameObject go = GameObject.FindGameObjectWithTag("Player");

		

		

		target = go.transform;

		maxDistance = 2;

		minDistance = 30;

		

		///////////////////////////////////waypoint stuff/////////////////////////////////////////

		targets =new List<Transform>();

		selectedTarget = null;

		myTransform = transform;

		AddAllWayPoints();

	}



	// Update is called once per frame

	void Update ()

	{

			

		if(CanSeeEnemy() == false)

		{ Debug.Log(" I can not see the target");

			

			TargetWaypoint();}

		

		

		else if ( CanSeeEnemy() == true)

			

		{ Debug.Log (" I am presuiting the target");

		Debug.DrawLine(target.position, myTransform.position,Color.red);

			LookAtTarget();

			Presuit();

			

	

		 if (Vector3.Distance(target.position, myTransform.position) < maxDistance)

			AttackTarget();

		}

		

			

		

	}

	///////////////////////////////////////////////////////functions start here///////////////////////////////////////

	private bool CanSeeEnemy()

	{if(Vector3.Distance(target.position, myTransform.position) > maxDistance  Vector3.Distance(target.position, myTransform.position) < minDistance)

		return true;

		else return false;

	}

	

	private void LookAtTarget()

	{

		myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position),rotationSpeed *Time.deltaTime);

	}

	private void Presuit()

	{

		myTransform.position += myTransform.forward * moveSpeed *Time.deltaTime;

			animation.CrossFade("run");

	}

	

	private void AttackTarget()

	{ random = Random.Range( 1,4);

		

		

	Debug.Log (" i am attacking the target");

		if( random ==3)

		{

			animation.CrossFade("attack01");

		}

		else if (  random ==2)					//this needs patching

		{

			animation.CrossFade("spattack01");

		}

		

		else 

		{

			animation.CrossFade("spattack02");

		}

		

		

	}

	////////////////////death routine////////////////////////////////////////////////////////

	private void Death()

	{

		 animation.CrossFade("death");

	}

	

	///////////////////////////////////// way point stuff starts here ///////////////////////////////////////

	

	

	

	public void AddAllWayPoints()

	{

		GameObject[] go = GameObject.FindGameObjectsWithTag("Waypoint");

		foreach(GameObject Waypoint in go)

			AddTarget(Waypoint.transform);

	}

	

	

	

	

		public void AddTarget(Transform Waypoint)

	{

		targets.Add(Waypoint);

	}

	

	

	private void SortTargetsByDistance()

	{

		targets.Sort(delegate(Transform t1, Transform t2)

			{

			return (Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)));

		});

		}

	

	

	private void LookAtWaypoint(  Transform w)

		

	{

		

		myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(w.position - myTransform.position),rotationSpeed *Time.deltaTime);

		

		Debug.Log("the way point" + w);

	}

	

	private void RunTowardsNextWaypoint( Transform w)

		

	{

		//if(Vector3.Distance(w.position, myTransform.position) > maxDistance  Vector3.Distance(w.position, myTransform.position) < minDistance)

		

		if(Vector3.Distance(w.position, myTransform.position)> 1)

		{myTransform.position += myTransform.forward * moveSpeed *Time.deltaTime;

		//Vector3.MoveTowards (targets );

	//myTransform.Translate (targets);

			animation.CrossFade("run");

			Debug.Log ("i am attempting to run but am failing");

		}

	}

	

	

	private void TargetWaypoint()

	{

		if(targets.Count == 0 )

		AddAllWayPoints();

		

		

		if(targets.Count > 0) {

			

			if(selectedTarget ==null)

			

		{

			SortTargetsByDistance();

			selectedTarget = targets[0];

			RunTowardsNextWaypoint(selectedTarget);

				Debug.Log (" I have reached my waypoint");

				animation.CrossFade("idle");

				

		}

			

		else

		{

				

			int index = targets.IndexOf(selectedTarget);

			if (index < targets	.Count	-1)

			{

				index ++;

				

			}

				

			else

			{

				index = 0;

			}

			selectedTarget = targets[index];

			Debug.Log (" I dont know what to do at waypoint");

		}

	

			

		}

				

	}

	
	

}

any hlep is good

I didn’t look at your code, but in general, problems like this can be (and usually are) solved via debugging.

Assuming you’re using Unity 3.x, you have at least a couple of options available for debugging: using MonoDevelop, and using ‘print’ debugging, where you simply add ‘print’ statements to your code (e.g. using Debug.Log()) that give you information about what’s going on.

Although the ‘print’ method is pretty low-tech, it’s not necessarily ineffective. Either way though, you’ll want to start to gather information about what’s actually happening in your code, and start narrowing things down as to what the cause of the problem might be. Almost certainly, after some time spent debugging you’ll stumble upon whatever it is that’s causing your AI agents not to patrol.

I realize I didn’t answer your question directly, but debugging is a necessary and invaluable skill, and is really how these sorts of problems are (and should be) solved in general.

[Edit: Also, if you do want people to look at your code, please edit or re-post it and fix the formatting (it’s very hard to read as it is now).]

While I don’t have access to my script where I do literally the same thing, best I can do for you is offer the process that my script does go through to achieve this.

currentRange = Vector3.Distance(player.position transform.position); // Using this I grab the distance between the player and the enemy. player.position is used as a transform variable

if (currentRange <= attackRange)//Attack range is the range the enemy will attack the player if they get this close
{
     Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime);//Makes the enemy move towards the player.
}
else if (currentRange >= attackRange)
{
     //Insert your patrol lines here
}
 public Transform[] patrolPoints; //Assign your patrol points with various game objects

For me right now, though I may change it, I have the enemy randomly move between one patrol point and another.

if (transform.position != patrolPoint[nextPoint])//If the enemy is NOT at its nxt patrol point, then it will move there
{
    Vector3.MoveTowards(transform.position, patrolPoints[nextPoint].position, speed * Time.deltaTime);
}
else if (transform.position == patrolPoints[nextPoint])//if the enemy is at its patrol point, then it finds a new one
{
    nextPoint = Random.Range(0,patrolPoints.Length);
}