Long Range problem

I added a new variable, distance gap so that my player can attack in long range, but when the enemy moves towards the player, it moves away.

This is what I added on the SearchPath function.

Vector3 targetPosition = target.position + (transform.position - target.position).normalized * distanceGap;

if((transform.position - target.position).normalized>distanceGap)){
Vector3 targetPosition = target.position + (transform.position - target.position).normalized * distanceGap;
}

1 Like

Hi

Your code tries to always keep the AI at a fixed distance away. Maybe you want to use Vector3.ClampMagnitude instead of normalized*distanceGap?

1 Like

Ohhhh. I see! Now it works. I didn’t know there was this function. Thanks Aron! :slight_smile: