IEnumerator FollowPath() { targetNodeIndex = 0; Vector3 currentNodePos = posPathToFollow[0]; while (true) { if (transform.position == currentNodePos) { //we have arrived at a location. At this point it will be either a node or an inserted bridge point. We need to check the former to see if it is visible by other units by accessing the node via the navgrid dictionary. if (pathfinder.GetNodeFromGridPosition(currentNodePos) != null) { Node currentNode = pathfinder.GetNodeFromGridPosition(currentNodePos); if (currentNode.GetDetectionLevel()) { //TODO. Unit is visible. Should probably do something about that } currentNode.SetShowPathIndicator(false); } targetNodeIndex++; //if next position is higher than this position, move up. Insert a new position first then continue //continuing. if (targetNodeIndex >= posPathToFollow.Count) { yield break; } currentNodePos = posPathToFollow[targetNodeIndex]; } transform.position = Vector3.MoveTowards(transform.position, currentNodePos, moveSpeed * Time.deltaTime); facingDirection = Vector3.Normalize(transform.position - currentNodePos); yield return(null); } }
// Update is called once per frame void Update() { currentUnit = unitHandler.GetCurrentlyActiveTurnUnit(); //only do this if it is the players turn if (currentUnit.GetFaction() == Faction.Player) { currentNavAgent = currentUnit.GetComponent <NavAgent>(); if (currentNavAgent.GetIsMoving()) { return; } startPosition = currentUnit.transform.position; maxDistanceToSample = currentUnit.GetAvailableAP() + 2.0f; if (EventSystem.current.IsPointerOverGameObject())//Check to make sure we are not over a UI element. { return; } RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f, navLayerMask)) { //Vector3 tempPos = hit.transform.position; Vector3 hitLocation = hit.point; //TODO: Do a distance check to make sure we don't sample too long a path. if (pathFinder.GetNodeFromGridPosition(hitLocation) != null) //if (hit.transform.GetComponent<Node>()) { targetPosition = pathFinder.GetNodeFromGridPosition(hitLocation).GetGridPosition(); UpdateDestinationCursor(targetPosition, true); //Update the elevation controller with the ypos of the destination cursor. if (endPosition != targetPosition) { linearDistanceToTarget = Vector3.Distance(startPosition, targetPosition); if (linearDistanceToTarget < maxDistanceToSample) { endPosition = targetPosition; RequestPath(); } return; } if (Input.GetButtonDown("LeftClick")) { currentUnit.GetComponent <NavAgent>().MoveNavAgent(finalPath); } } else if (!hit.transform.GetComponent <Node>()) { if (isDestinationCursorActive) { UpdateDestinationCursor(targetPosition, false); } } } } }