Question by 
               JulianHspl · May 09, 2016 at 02:54 PM · 
                enemy aifollow player  
              
 
              Enemy AI chase, then attack player
Hi, I'm working on an zelda like unity2d game right now. There is an enemy which follows the player. Now I want that the enemy stops when he's in front of the player and attack him, but I don't know how to force him to stop at a distance. Can someone help me ?
This is the script:
 public class BatChasesPlayer : MonoBehaviour 
 {
     public float speed;
     private Vector3 player;
     private Vector2 playerDirection;
     private float xDif;
 
     private float yDif;
     public Rigidbody2D rb2d;
 
     private Animator anim;
 
     void Start() {
     
 //        anim = GetComponent<Animator> ();
     
     }
 
 
     void Update() {
     
         player = GameObject.Find ("Player(Clone)").transform.position;
 
 
 
         //player = GameObject.FindGameObjectWithTag ("Player").transform.position;
 
         xDif = player.x - transform.position.x;
         yDif = player.y - transform.position.y;
 
         playerDirection = new Vector2 (xDif, yDif);
 
 //        StartCoroutine("waitTwoSeconds");
 
 
 
         rb2d.AddForce (playerDirection.normalized * speed); //amount of force doesnt depend on distance(normalized)
 
     }
 
 
     }
 
               Comment
              
 
               
              Answer by magicalunicornart · Jun 19, 2019 at 10:30 PM
For something like this you should use raycast (checking far away) or overlapcircle (more close) or overlapbox
Your answer
 
 
             Follow this Question
Related Questions
Enemy follow player 0 Answers
Enemys stop following after destroying gameobject 1 Answer
How to evade Enemy AI? 1 Answer
Enemy doesn't follow player clone,Enemy doesn't follow the player clone 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                