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)
}
}