Enemy 2d attacking

Ok so i just made an enemy, But with this script the enemy basically just jump on me one time, then go back to the patrol points i set for it then never go on me again. I need help to make the enemy actually follow me and attack me when in range.

using UnityEngine;
using System.Collections;

public class zombiepatrol : MonoBehaviour {

	public Transform[] patrolpoints;
	int currentPoint;
	public float speed=0.5f;
	public float timestill=2f;
	public float sight=3f;
	Animator anim;
	public float force;

	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
		StartCoroutine ("Patrol");
		Physics2D.queriesStartInColliders = false;

	// Update is called once per frame
	void Update () {

		RaycastHit2D hit= Physics2D.Raycast (transform.position, transform.localScale.x * Vector2.right, sight);
		if (hit.collider != null && hit.collider.tag == "Player")
			GetComponent<Rigidbody2D> ().AddForce (Vector3.up*force + (hit.collider.transform.position-transform.position)*force);

	IEnumerator Patrol()
		while (true) {

			if(transform.position.x== patrolpoints[currentPoint].position.x )
				yield return new WaitForSeconds(timestill);

			if(currentPoint >=patrolpoints.Length)

			transform.position=Vector2.MoveTowards(transform.position,new Vector2(patrolpoints[currentPoint].position.x,transform.position.y),speed);

			if(transform.position.x> patrolpoints[currentPoint].position.x)
				transform.localScale=new Vector3(-1,1,1);
			else if (transform.position.x< patrolpoints[currentPoint].position.x)
				transform.localScale= Vector3.one;

			yield return null;


	void OnTriggerEnter2D(Collider2D other)
		if (other.tag == "Projectile")
			Destroy (this.gameObject, 0.1f);

	void OnDrawGizmos()
		Gizmos.color = Color.red;

		Gizmos.DrawLine (transform.position, transform.position + transform.localScale.x * Vector3.right * sight);



First change this (hit.collider != null && hit.collider.tag == "Player")

to this ( hit.collider.tag == "Player")
thats enough and you wont Get in trouble in the future.

Next add a public Transform yourPlayerand assign it, And public float speed; .
Next instead of this GetComponent<Rigidbody2D> ().AddForce (Vector3.up*force + (hit.collider.transform.position-transform.position)*force);

use float step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, yourPlayer.position, step);

kind regards hope it helps.

With things that need to persist over frames, you usually want to define a property somewhere in your class. I have a system in my game where I define an enumerator, like this:

public enum AIAction {
    Attack, Patrol, GoToWayPoint, Idle // etc...

I then create a property in my enemy class and assign to it depending on the situation. Then, I make the AI do things based on it’s current action.

public class Enemy {
    public AIAction currentAction { get; private set; }

    void Update () 
        if (// enemy is detected) 
            currentAction = AIAction.Attack;
            target = detectedUnit;

        switch (currentAction) 
            case AIAction.Attack:
                // attack the target
            // create a case for your other actions

With that said, I don’t find that this is a good option for my uses. My game involves group vs. group battles, and if everyone focused their attacks on one target out of the whole group it would make things uninteresting.

Instead, what I’ve done is created a hivemind script for each of the factions. When a unit detects an enemy, instead of making the decision to attack the target itself, it tells the hivemind about the enemy. The hivemind then allocates units as necessary to attack that enemy by comparing the enemy’s strength to the strength of the allied units attacking it. It keeps a list of all the enemies and all of the units attacking each of those enemies. this creates a cool dynamic where if I have 20 or so enemies on screen, there are multiple tiny battles taking place instead of all units from all factions aiming at the closest enemy.