having some trouble keeping enemies at a distance from player in combat. I can get the enemies to run towards the player to get within attack range, but when i move closer to the enemy i want them to run away to their range distance. Any help would be much appreciated
public Transform target;
public float speed = 0.5f;
private float minDistance = 4f;
private float range;
void Start () {
target = GameObject.FindWithTag("Player").transform;
}
void update() {
range = Vector2.Distance(transform.position, target.position);
if (range > minDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}