private void ComputeAStarForSteering() { Node npcNode = NavigationGraph.GetClosestNode(NpcManager.transform.position); Node playerNode = NavigationGraph.GetClosestNode(_playerData.LastSeenPosition); List <Node> path = Pathfinding.Astar(npcNode, playerNode); if (path == null || path.Count < 1) { Debug.Log(npcNode); return; } _currentTargetLocation = path[NpcGlobalVariables.NextElementInPath].position; }
private void Update() { if (_agents.Count < 1) { return; } if (_agentReachedNode || !Node.CanSeeNode(NavigationGraph.GetClosestNode(_currentTargetLocation), GetPositionOfClosestAgent())) { ComputeAStarForFlock(); _agentReachedNode = false; } MoveAgents(); }
private void ComputeAStarForFlock() { Node npcNode = NavigationGraph.GetClosestNode(GetPositionOfClosestAgent()); Node playerNode = NavigationGraph.GetClosestNode(_playerData.LastSeenPosition); List <Node> path = Pathfinding.Astar(npcNode, playerNode); pathTest = path; if (path == null) { Debug.Log($"The A* path is null. Last seen player position is {_playerData.LastSeenPosition}"); } if (path.Count < 2) { _currentTargetLocation = _playerData.LastSeenPosition; } else { _currentTargetLocation = path[NpcGlobalVariables.NextElementInPath].position; } }
private void FindNewTarget() { Vector3 npcPosition = NpcManager.transform.position; Vector3 npcVelocity = NpcManager.Rigidbody.velocity; Vector3 circlePosition = npcPosition + Vector3.Normalize(npcVelocity) * NpcGlobalVariables.WanderCircleDistance; Vector3 target = circlePosition + Random.insideUnitSphere * NpcGlobalVariables.WanderCircleRadius; target.y = 0; Node currentNode = NavigationGraph.GetClosestNode(npcPosition); Node targetNode = NavigationGraph.GetClosestNode(target); try { _pathToTarget = new Queue <Node>(Pathfinding.Astar(currentNode, targetNode)); } catch (Exception) { Debug.Log(NpcManager.name + " tried to go on a separated graph. Will stay Idle."); NpcManager.GetComponent <NPCStateMachine>().StartIdle(); } if (_pathToTarget == null || _pathToTarget.Count < 1) { NpcManager.GetComponent <NPCStateMachine>().StartIdle(); return; } _currentTargetNode = _pathToTarget.Dequeue().position; _currentTargetNode.y = 0; _reachedTarget = false; }
// Check for new links inside the cluster public bool CheckNewLinks(List <Node> cluster, bool canCreateNew = true) { bool addedNewNodes = false; foreach (var node in cluster.ToArray()) { // The ToArray is to iterate over a copy of the list, and so we can add new items while iterating Vector3 tmp_dir = (node.position - position); float dist = tmp_dir.magnitude; if (CanSeeNode(node, dist, tmp_dir)) { // If we can see the node, link the 2 nodes together // // Uncoment theses 2 lines to keep the graph simple with big links. // // Will generate faster, but will be less precise for AI movement // AddLink(node, dist); // continue; // if dist > nodesMaxDistance, add in between nodes if (dist > NavigationGraph.maxLinkLength && dist < NavigationGraph.nodesMaxDistance && canCreateNew && false) // Disable this for now { int nb = (int)(dist / NavigationGraph.maxLinkLength); // compute nb of nodes to add Vector3 dir = (node.position - position).normalized; float intermediateDist = dist / (nb + 1); // distance between nodes Node prev = this; // Keep track of the previous node to add in between links while (nb > 0) { Vector3 pos = prev.position + dir * intermediateDist; Node tmp = NavigationGraph.CreateNode(pos, cluster); if (tmp == null) { // If we fail creating an intermediate node, try to use the closest one Node close = NavigationGraph.GetClosestNode(pos); if (close != null && (close.position - pos).magnitude < NavigationGraph.wallOffset) { tmp = close; } else { // if we can't have a close node, just keep a long link and don't bother more prev.AddLink(node); break; } } addedNewNodes = true; // No need to raycast to check the link : we already did one before on a longest distance // (we already know that the fartest nodes can see each other) tmp.AddLink(prev); prev = tmp; nb--; } prev.AddLink(node); // don't forget to link the last node } else if (dist < NavigationGraph.nodesMaxDistance) { AddLink(node, dist); } } } return(addedNewNodes); }