Question by 
               Fakemaster · Mar 13, 2017 at 05:41 PM · 
                c#scripting problemtransform.position  
              
 
              Enemy is following my player but dont stop
Sooo i my Enemy is following my Player, but i want that it stops before the Enemy is over my Player.
This is my Script.
 private GameObject Player;
 
      void Start () {
          Player = GameObject.Find("Player");
      }
      
      void Update () {
          transform.position = Vector2.MoveTowards(transform.position,Player.transform.position, speed*Time.deltaTime);
      }
 
               
This is the first step, but if i stop moving my player

And than it´s that, i want that the enemy stops before it goes over my Player
 
                 
                unbenannt.png 
                (132.0 kB) 
               
 
                
                 
                unbenannt1.png 
                (78.2 kB) 
               
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Commoble · Mar 13, 2017 at 05:50 PM
You can use Vector2.Distance to get the distance between two positions. Then, knowing this distance, you can decide whether or not to move closer. A basic example:
 public float minDistance;    // you can set this in the Inspector
 
 void Update()
 {
     if (Vector2.Distance (this.transform.position, Player.transform.position) > minDistance)
     {
         transform.position = Vector2.MoveTowards(transform.position,Player.transform.position, speed*Time.deltaTime);
     }
 }
 
               You can also use Vector3.Distance for objects in 3D space. Vector2 just ignores the z-axis.
Your answer