void AcquireTarget() { if (targetObject == null) { GameObject closestEnemy = null; foreach (GameObject go in UnitTracker.GetActiveEnemies(gameObject)) { if (closestEnemy == null) { closestEnemy = go; continue; } if ((go.transform.position - transform.position).magnitude < (closestEnemy.transform.position - transform.position).magnitude) { closestEnemy = go; } } targetObject = closestEnemy; if (targetObject != null) { target = targetObject.transform.position; } } }
// Gets the enemy closest to crosshairs GameObject GetEasiestTarget() { GameObject easiestTarget = null; foreach (GameObject enemy in UnitTracker.GetActiveEnemies(myInfo.TeamID)) { if ((easiestTarget == null) || (Vector3.Angle(transform.forward, easiestTarget.transform.position - transform.position) > Vector3.Angle(transform.forward, enemy.transform.position - transform.position))) { easiestTarget = enemy; } } targetTimer = 0; return(easiestTarget); }
// Gets the closest enemy GameObject GetClosestTarget() { GameObject closestTarget = null; foreach (GameObject enemy in UnitTracker.GetActiveEnemies(myInfo.TeamID)) { if ((closestTarget == null) || (Vector3.Distance(enemy.transform.position, this.transform.position) > Vector3.Distance(closestTarget.transform.position, this.transform.position))) { closestTarget = enemy; } } targetTimer = 0; return(closestTarget); }
/// <summary> /// Returns the closest enemy of given UnitTypes that is within the given maximum distance. /// </summary> /// <param name="myUnit">Unit to find the enemies of.</param> /// <param name="unitTypes">Array of unit types that are valid targets.</param> /// <param name="maxDistance">Maximum distance to find enemies.</param> /// <returns>Closest enemy unit within max distance.</returns> public static GameObject GetClosestEnemy(GameObject myUnit, UnitType[] unitTypes, int maxDistance, bool objectiveUnitsOnly) { List <GameObject> enemies; if (objectiveUnitsOnly) { enemies = UnitTracker.GetObjectiveEnemies(myUnit); } else { enemies = UnitTracker.GetActiveEnemies(myUnit); } GameObject closestEnemy = null; float closestEnemyDistance = float.MaxValue; foreach (GameObject go in enemies) { bool isCorrectShipType = false; foreach (UnitType ut in unitTypes) { if (go.GetComponent <UnitInfo>().UnitType == ut) { isCorrectShipType = true; break; } } if (!isCorrectShipType) { continue; } float dist = (go.transform.position - myUnit.transform.position).magnitude; if (dist < closestEnemyDistance && dist <= maxDistance) { closestEnemyDistance = (go.transform.position - myUnit.transform.position).magnitude; closestEnemy = go; } } return(closestEnemy); }
/// <summary> /// Find the closest enemy unit and sets it as our target. /// </summary> void AcquireTarget() { if (!ownerAndTeamSet) { return; } if (targetObject == null || targetObject.activeInHierarchy == false) { //If our owner is the player: Set our target as the player's target. if (GetComponent <ProjectileInfo>().Owner == UnitTracker.PlayerShip && TargettingController.Instance != null) { targetObject = TargettingController.Instance.Target; } if (targetObject == null) { GameObject closestEnemy = null; foreach (GameObject go in UnitTracker.GetActiveEnemies(gameObject)) { if (closestEnemy == null) { closestEnemy = go; continue; } if ((go.transform.position - transform.position).magnitude < (closestEnemy.transform.position - transform.position).magnitude) { closestEnemy = go; } } targetObject = closestEnemy; } if (targetObject != null) { target = targetObject.transform.position; } } }
void SwitchTarget() { //Get the list of enemies... List <GameObject> units = UnitTracker.GetActiveEnemies(); //cycle through the list of enemies... for (int i = 0; i < units.Count; i++) { if (units [i] != null && units [i].GetComponent <UnitInfo> () != null) { //First, we need to check if we currently have a target. If we don't just assign the first one for now if (target == null || !target.activeInHierarchy) { target = units [i]; targetIndex = i; } //If the current unit being checked ISN'T the target and is closest to the reticle then pick that one targetLoc = playerCam.WorldToScreenPoint(units[i].transform.position); Vector2 currentTargetLoc = playerCam.WorldToScreenPoint(target.transform.position); //We also want to check if the target is within the camera's view. if (target != units [i] && (units[i].GetComponent <Renderer>().isVisible)) { //Now, we check to see if the new target is CLOSER to the reticle than the current target. If so, switch targets. if (Mathf.Sqrt((targetLoc.x - (Screen.width / 2f)) * (targetLoc.x - (Screen.width / 2f)) + (targetLoc.y - (Screen.height / 2f)) * (targetLoc.y - (Screen.height / 2f))) < //if distance1 from center is less than Mathf.Sqrt((currentTargetLoc.x - (Screen.width / 2f)) * (currentTargetLoc.x - (Screen.width / 2f)) + (currentTargetLoc.y - (Screen.height / 2f)) * (currentTargetLoc.y - (Screen.height / 2f)))) // Distance 2... //After that sinful line of code we assign the target if the current one being checked is closest to the center of the screen (reticle) { target = units [i]; targetIndex = i; targetTitle.text = target.GetComponent <UnitInfo> ().ShipTitle; targetIcon.GetComponent <AudioSource> ().Play(); } } } } }
void FixedUpdate() { if (playerObj != null) { int playerTeamID = -1; if (playerObj.GetComponent <UnitInfo>() != null) { playerTeamID = playerObj.GetComponent <UnitInfo>().TeamID; } //We update the player's location every physics step playerLoc = playerObj.transform.position; //Get the active enemies and iterate through the list List <GameObject> enemies = UnitTracker.GetActiveEnemies(); int requiredEnemyIcons = 0; for (int i = 0; i < enemies.Count; i++) { if (enemies[i] != null && enemies[i].GetComponent <UnitInfo>() != null) { //Calculate the distance of each enemy from the player and check if it's outside the range of the radar Vector2 distFromPlayer = new Vector2(playerLoc.x - enemies[i].transform.position.x, playerLoc.z - enemies[i].transform.position.z); float length = Mathf.Sqrt((distFromPlayer.x * distFromPlayer.x) + (distFromPlayer.y * distFromPlayer.y)); if (length < radarRange) { //Draw the unit on the radar, maintain scale, rotation etc Vector3 pos = new Vector3((transform.position.x - distFromPlayer.x / (radarRange / 150)), (transform.position.y - distFromPlayer.y / (radarRange / 150)), transform.position.z); if (requiredEnemyIcons < enemyIcons.Count) { enemyIcons[requiredEnemyIcons].transform.position = pos; } else { enemyIcons.Add(PoolController.Instance.GetObject(enemyIcon, pos, Quaternion.identity, transform)); } requiredEnemyIcons++; } } } //Remove unneccessary enemy icons. for (int i = enemyIcons.Count - 1; i >= requiredEnemyIcons; i--) { enemyIcons[i].SetActive(false); enemyIcons.Remove(enemyIcons[i]); } List <GameObject> allies = UnitTracker.GetActiveAllies(); int requiredAllyIcons = 0; for (int i = 0; i < allies.Count; i++) { if (allies[i] == playerObj) { continue; } if (allies[i] != null && allies[i].GetComponent <UnitInfo>() != null) { //Calculate the distance of each enemy from the player and check if it's outside the range of the radar Vector2 distFromPlayer = new Vector2(playerLoc.x - allies[i].transform.position.x, playerLoc.z - allies[i].transform.position.z); float length = Mathf.Sqrt((distFromPlayer.x * distFromPlayer.x) + (distFromPlayer.y * distFromPlayer.y)); if (length < radarRange) { //Draw the unit on the radar, maintain scale, rotation etc Vector3 pos = new Vector3((transform.position.x - distFromPlayer.x / (radarRange / 150)), (transform.position.y - distFromPlayer.y / (radarRange / 150)), transform.position.z); if (requiredAllyIcons < allyIcons.Count) { allyIcons[requiredAllyIcons].transform.position = pos; } else { allyIcons.Add(PoolController.Instance.GetObject(allyIcon, pos, Quaternion.identity, transform)); } requiredAllyIcons++; } } } //Remove unneccessary ally icons. for (int i = allyIcons.Count - 1; i >= requiredAllyIcons; i--) { allyIcons[i].SetActive(false); allyIcons.Remove(allyIcons[i]); } //Get the player ships rotation Vector3 playerRotation = playerObj.transform.eulerAngles; //Rotate our radar with the player ship, but only on one axis transform.eulerAngles = new Vector3(transform.rotation.x, transform.rotation.y, -playerRotation.y); } }