Enemy go haywire after player approaching it, how to properly implement waypoint and chase player.

Hi guys i have assign in a way that if within the range the enemy will chase the player and if out of range will back to its waypoint. The problem is when it back to its waypoint and while my player near the enemy, it will go out of direction and other way then only back and chase player. Do you guys know what is the problem?

Enemy.cs

    public bullettower1 bulletPrefab = null;
    public Transform target;
    public int moveSpeed;
    public int rotationSpeed;
	
    float range = 10.0f;
	float range2 = 15.0f;
	float range3 = 4.0f;
	float range4 = 20.0f;
	
	
	float nextFire = 0;
	float fireRate = 1.5f;
	float stop = 3.0f;

	
Transform myTransform;
	
public Transform[] waypoints;
	 public int waypointId = 0;
	
	
    
	[SerializeField]
	public Transform hand;
    
	
	
    void Awake(){
		
        myTransform = transform;
    }

	// Use this for initialization
	void Start () {
        GameObject go = GameObject.FindGameObjectWithTag("Player");
		animation.CrossFade("idle");
        target = go.transform;
		Patrol ();

		
	}
	
	
	
	void Patrol(){
		
		
		
		if (Vector3.Distance(transform.position, waypoints[waypointId].position) < 2){
			
			waypointId++;
			
			if (waypointId >= waypoints.Length) waypointId = 0;
		}
			animation.CrossFade("run");		
           transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypoints[waypointId].position - transform.position), rotationSpeed*Time.deltaTime);
		   transform.position += transform.forward * moveSpeed * Time.deltaTime; 
			
			
			
			
		}	
	
	
	
	
	// Update is called once per frame
	void Update () {
		
      
    float distance = Vector3.Distance(myTransform.position, target.position);
    if (distance <= range2 &&  distance >= range){
	
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
	
    }
 
 
    else if(distance <= range && distance > stop){
  
    //move towards the player
	animation.CrossFade("run");		
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
	
   
    }
    else if (distance <=stop) {
	
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    }
	
	else if (distance >= range) {
	
	animation.CrossFade("idle");
		}

 
	if (distance <= range3)
		{
			
		    animation.CrossFade("shoot");
	        if (Time.time > nextFire)
	{
			nextFire = Time.time + fireRate;
			GameObject g = (GameObject)Instantiate(bulletPrefab.gameObject, hand.position, Quaternion.identity);
			
			bullettower1 b = g.GetComponent<bullettower1>();

                   
            b.setDestination(target.transform);	
				
			
			
			
				
				
	}	
			
			
		}
		
		else if (distance <= range4)
		
		{
			Patrol();
		
	
		
		
	}
}
	
}

Buying the AI made simple from Walker Boys studio is a great investment, could save you some time.