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
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.
It is working perfectly, thanks for the help 