- Home /
 
 
               Question by 
               EternalAmbiguity · Jun 21, 2017 at 06:28 AM · 
                c#navmeshnavmeshagentdelegateaction  
              
 
              How Do I Make A NavMeshAgent Move To Points Sequentially?
I'm working on a game with turn-based combat. I want to queue up actions (separate methods) before performing them all in sequence. However, when I try to do this with multiple "move to" locations for a navmeshagent, , it only goes to the final location (rather than moving through each destination sequentially).
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.AI;
 using System;
 
 public class TBCombatController : MonoBehaviour {
     NavMeshAgent playerAgent;
     GameObject combatUI;
 
     List<Action> actionQueue = new List<Action>();
     public bool combatActive = false;
     // Use this for initialization
     void Start () {
         playerAgent = GameObject.Find("Player").GetComponent<NavMeshAgent>();
         combatUI = GameObject.Find("CombatUI");
         combatUI.SetActive(false);
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject() && combatActive == true)
             GetInteraction();
     }
 
     void GetInteraction()
     {
         Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit interactionInfo;
         if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
         {
             GameObject interactedObject = interactionInfo.collider.gameObject;
             if (interactedObject.tag == "Interactable Object")
             {
                 actionQueue.Add( ()=> MoveAndInteract(interactedObject));
             }
             else
             {
                 actionQueue.Add(() => MoveUnit(interactionInfo));
             }
         }
         
         Debug.Log(actionQueue.Count);
     }
 
     void MoveAndInteract(GameObject interactedObject)
     {
         interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
     }
 
     void MoveUnit(RaycastHit interactionInfo)
     {
         playerAgent.stoppingDistance = 0f;
         playerAgent.destination = interactionInfo.point;
     }
 
     public void SetupCombatUI()
     {
         actionQueue.Clear();
         combatUI.SetActive(true);
         combatActive = true;
     }
 
     public void ExecuteTurn()
     {
         foreach (Action action in actionQueue)
         {
             action();
             Debug.Log("One method called.");
         }
         actionQueue.Clear();
     }
 
     public void ClearQueue()
     {
         actionQueue.Clear();
     }
 }
 
               As can be seen with my debug logs, I'm checking if the function gets called more than once. And it does! And the agent seems to pause for a beat when I click around multiple times. But ultimately it just moves to the final location.
I really have no idea how to fix this. Can anyone help me?
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Click to Move - get NavMesh Area Name 2 Answers
Unity pathfinding - Comparing 2 paths? 2 Answers
NavMeshAgent Destination Problem 0 Answers