private void LateUpdate() { if (currentAction != null && currentAction.isActionRunning) { //float distanceToTarget = Vector3.Distance(currentAction.target.transform.position, this.transform.position); if (currentAction.navAgent.hasPath && currentAction.navAgent.remainingDistance < 2.0f) { if (!invoked) { Invoke("CompleteAction", currentAction.duration); invoked = true; } } return; } if (planner == null || actionQueue == null) { planner = new GOAPPlanner(); var sortedGoals = from entry in goals orderby entry.Value descending select entry;//sort goals in order of priority foreach (KeyValuePair <Goal, int> sgoal in sortedGoals) { actionQueue = planner.plan(actions, sgoal.Key.subGoals, beliefs); if (actionQueue != null) { currentGoal = sgoal.Key; break; } } } if (actionQueue != null && actionQueue.Count == 0) { if (currentGoal.remove) { goals.Remove(currentGoal); } planner = null; } if (actionQueue != null && actionQueue.Count > 0) { currentAction = actionQueue.Dequeue(); if (currentAction.PrePerform()) { if (currentAction.target == null && currentAction.targetTag != "") { currentAction.target = GameObject.FindGameObjectWithTag(currentAction.targetTag); } if (currentAction.target != null) { currentAction.isActionRunning = true; currentAction.navAgent.SetDestination(currentAction.target.transform.position); } } else { actionQueue = null; } } }
void LateUpdate() { if (currentAction != null && currentAction.running) { if (currentAction.agent.hasPath && currentAction.agent.remainingDistance < 1f) { if (!invoked) { Invoke("CompleteAction", currentAction.duration); invoked = true; } } return; } if (planner == null || actionQueue == null) { planner = new GOAPPlanner(); var sortedGoals = from entry in goals orderby entry.Value descending select entry; foreach (KeyValuePair <SubGoal, int> sg in sortedGoals) { actionQueue = planner.plan(actions, sg.Key.sgoals, null); if (actionQueue != null) { currentGoal = sg.Key; break; } } } if (actionQueue != null && actionQueue.Count == 0) { if (currentGoal.remove) { goals.Remove(currentGoal); } planner = null; } if (actionQueue != null && actionQueue.Count > 0) { currentAction = actionQueue.Dequeue(); if (currentAction.PrePerform()) { if (currentAction.target != null) { currentAction.running = true; currentAction.agent.SetDestination(currentAction.target.transform.position); } } else { actionQueue = null; } } }