[SOLVED] NaveMeshAgent stop in range - RPG Like (Ragnarok ref)

It’s the very first time i’m using nave mash & path finding stuff so i am having some issues and probably doing things wrong, for like 3 days…

  • Everything with navigation is fine, but i need some extra stuff and i am not being able to make in work right. The thing is: my player should walk to an enemy and when the distance between them is less or equal to my player range it should stop.

  • The piece of code i’m about to show is working correctly in this first aspect: the player goes close to the enemy and once in range it stop and start attacking, but the problem is when the enemy is killed, the player get stuck and can’t move. I don’t know why.

  • Extra: It should be able to move freely if there is no target (which occurs before we actually kill the enemy). Once it start attacking - or get a target - it can’t move (we don’t want that).

(It’s a RPG like kinda of thing. I’m using Ragnarok as reference)

    void MovePlayer()
    {
        agent.SetDestination (targetPos);

        if (target != null) {
            if (Vector3.Distance (transform.position, target.transform.position) <= atkRange) {
                agent.Stop ();
            }
        }
    }

you are not resuming your agent.

use agent.Resume() when the target == null (because he is dead at that moment and shouldn’t find anything anymore)

Hope this helps

1 Like

It works! Thanks :slight_smile: