Search Unity

NavMeshAgent: Variable to check if there is a destination Logged?

Discussion in 'Scripting' started by muzzyandmonkey1, Apr 18, 2014.

  1. muzzyandmonkey1

    muzzyandmonkey1

    Joined:
    Jul 26, 2013
    Posts:
    2
    Hello Unity Forums!
    I have a zombie-survival style game I am working on for a bit of fun at the moment but I've hit a problem.
    I want to have a certain Mecanim animation play if the Zombie A.I. NavMeshAgent doesn't doesn't have a destination.
    Here's my code:
    Code (csharp):
    1.  
    2. if(/*Agent.hasDestination = true?*/)
    3. {
    4.     print("Debug: Idle = True");
    5.     animator.SetBool("Idle", true);
    6. }
    I did try checking if the Agent.destination variable was null like so:
    Code (csharp):
    1. if(Agent.destination == null){}
    But that didn't seem to work so I printed the variable into the console, to find that if there is no destination set, it holds the starting position of the agent as a Vector 3 Coordinate, so I set the starting position of the zombie to a variable in the Start function and then put this in the if Statement:
    Code (csharp):
    1. if(Agent.destination == StartPos){}
    However this still didn't work as I realised that the destination was slightly different on the Y Axis as I had the GameObject slightly above the ground and the destination had the Y on the ground with the NavMesh.

    Hope you can help with my problem:D

    P.S.: I probably won't reply back to anything in the next couple of days because I'm going back where there is no internet.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You could theoretically check if hasPath is true or not. Depends on whether or not Unity clears the path after the agent arrives (or you can clear it yourself with ResetPath). You could also check if remainingDistance is very close to 0.
     
  3. muzzyandmonkey1

    muzzyandmonkey1

    Joined:
    Jul 26, 2013
    Posts:
    2
    Thanks for the quick reply! It completely didn't occur to me I could do that.

    Thanks!

    :D