public CoverSpot GetCoverTowardsTarget(Soldier soldier, Vector3 targetPosition, float maxAttackDistance, float minAttackDistance, CoverSpot prevCoverSpot) { CoverSpot bestCover = null; Vector3 soldierPosition = soldier.transform.position; CoverSpot[] possibleCoverSpots = unOccupiedCoverSpots.ToArray(); for (int i = 0; i < possibleCoverSpots.Length; i++) { CoverSpot spot = possibleCoverSpots[i]; if (!spot.IsOccupied() && spot.AmICoveredFrom(targetPosition) && Vector3.Distance(spot.transform.position, targetPosition) >= minAttackDistance && !CoverIsPastEnemyLine(soldier, spot)) { if (bestCover == null) { bestCover = spot; } else if (prevCoverSpot != spot && Vector3.Distance(bestCover.transform.position, soldierPosition) > Vector3.Distance(spot.transform.position, soldierPosition) && Vector3.Distance(spot.transform.position, targetPosition) < Vector3.Distance(soldierPosition, targetPosition)) { if (Vector3.Distance(spot.transform.position, soldierPosition) < Vector3.Distance(targetPosition, soldierPosition)) { bestCover = spot; } } } } if (bestCover != null) { bestCover.SetOccupier(soldier.transform); AddToOccupied(bestCover); } return(bestCover); }
void stateMoveToCover() { if (curTarget != null && currentCover != null && currentCover.AmICoveredFrom(curTarget.transform.position)) { if (currentPath != null) { Soldier alternativeTarget = GetNewTarget(); if (alternativeTarget != null && alternativeTarget != curTarget) { float distanceToCurTarget = Vector3.Distance(myTransform.position, curTarget.transform.position); float distanceToAlternativeTarget = Vector3.Distance(myTransform.position, alternativeTarget.transform.position); float distanceBetweenTargets = Vector3.Distance(curTarget.transform.position, alternativeTarget.transform.position); if (Mathf.Abs(distanceToAlternativeTarget - distanceToCurTarget) > 5 && distanceBetweenTargets > 5) { curTarget = alternativeTarget; coverManager.ExitCover(currentCover); currentCover = coverManager.GetCoverTowardsTarget(this, curTarget.transform.position, maxAttackDistance, minAttackDistance, currentCover); currentPath = CalculatePath(myTransform.position, currentCover.transform.position); return; } } if (currentPath.ReachedEndNode()) { //if we reached the end, we'll start looking for a target anim.SetBool("move", false); currentPath = null; state = ai_states.combat; return; } Vector3 nodePosition = currentPath.GetNextNode(); if (Vector3.Distance(myTransform.position, nodePosition) < 1) { //if we reached the current node, then we'll begin going towards the next node currentPath.currentPathIndex++; } else { //else we'll move towards current node myTransform.LookAt(nodePosition); myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); } } else { //if we don't have a path, we'll look for a target anim.SetBool("move", false); state = ai_states.idle; } } else { anim.SetBool("move", false); state = ai_states.idle; } }