Search Unity

NavMeshAgent keeps walking and I don't know why

Discussion in 'Navigation' started by HeaDiii, Oct 22, 2017.

  1. HeaDiii

    HeaDiii

    Joined:
    May 18, 2015
    Posts:
    61
    Hey there,

    I'm currently programming some kind of AI for an RTS-Game and I have a Character that keeps going even though it reached its destination. I'm using a little state machine to make it easier to decide what to do next. Here is my Code:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         switch (currentBuilderState)
    4.         {
    5.                 case BuilderState.WalkingToTarget:
    6.                     WalkToBuilding();
    7.                     break;
    8.                 case BuilderState.Building:
    9.                     ShowBuildingAnimation();
    10.                     break;
    11.                 case BuilderState.None:
    12.                     break;
    13.         }
    14.     }
    15.  
    16.     private void WalkToBuilding()
    17.     {
    18.         character.Move(agent.desiredVelocity, false, false);
    19.         if (agent.remainingDistance < agent.stoppingDistance && !_builderPosition.inPlace)
    20.         {
    21.             StartBuilding();
    22.         }
    23.     }
    24.  
    25.     private void StartBuilding()
    26.     {
    27.         // Reached BuilderPosition of Building
    28.         Debug.Log("reached builderPosition");
    29.         transform.rotation = _builderPosition.transform.rotation;
    30.         _builderPosition.inPlace = true;
    31.         target = null;
    32.         agent.isStopped = true;
    33.      
    34.         currentBuilderState = BuilderState.Building;
    35.     }
    As you can see, I only use agent.Move while the WalkingToTarget-State is active. As soon as the agent reaches its target, the function StartBuilding is executed (only once!), which changes the state to Building. Inside the function StartBuilding I try to stop the agent but he keeps running a bit further until he stops while remaining in the running animation.

    Can someone tell me what I am doing wrong?

    EDIT: ShowBuildingAnimation is empty right now.
     
  2. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    I could be wrong but if you're not setting a destination but only telling the agent to move there is no stopping distance .why not use set destination instead of move.
     
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    dont use move use set destination. Your telling it to continiously move instead of giving it a place to path to. otherwise dont use a navmesh and just use a rigidbody, which is effectively what your using the agent as right now.
     
  4. HeaDiii

    HeaDiii

    Joined:
    May 18, 2015
    Posts:
    61
    If I'm not using Move, my agent just weirdly gets translated to the destination without updating the rotation or beeing animated.

    I solved my issue by using
    Code (CSharp):
    1. Move(Vector3.zero, false, false);
    Not the most elegant way but it does the job.