Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by frozendog_bren, Sep 1, 2015.

  1. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    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)

    Code (CSharp):
    1.     void MovePlayer()
    2.     {
    3.         agent.SetDestination (targetPos);
    4.  
    5.         if (target != null) {
    6.             if (Vector3.Distance (transform.position, target.transform.position) <= atkRange) {
    7.                 agent.Stop ();
    8.             }
    9.         }
    10.     }
     
  2. SNIPEZPUNISHER

    SNIPEZPUNISHER

    Joined:
    Jun 23, 2015
    Posts:
    9
    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
     
    frozendog_bren likes this.
  3. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    It works! Thanks :)