public void KilledTarget(UnitControl victim) { Debug.Log(transform.name + " killed " + victim.name); if (victim.Equals(currentTarget)) { currentTarget = null; if (State == "Attacking") { State = "Waiting"; } } }
public UnitControl ScanForTargets(UnitControl excludeThisGuy) { GameObject[] possibleTargets = GameObject.FindGameObjectsWithTag("Unit"); LayerMask layerMask = 1 << LayerMask.NameToLayer("Terrain"); float closestDistance = Mathf.Infinity; GameObject closestTarget = null; foreach (GameObject target in possibleTargets) { if (target.name.Contains(unitControl.TeamName)) { continue; } UnitControl targetControl = target.GetComponent <UnitControl>(); if (!targetControl || targetControl.IsDead) { continue; } if (excludeThisGuy && excludeThisGuy.Equals(targetControl)) { continue; } float targetDistance = Vector3.Distance( target.transform.position, transform.position ); if (targetDistance > visualRange) { continue; } if (closestTarget && closestDistance < targetDistance) { continue; } Ray ray = new Ray( transform.position + (Vector3.up * 0.75f), (target.transform.position - transform.position).normalized ); Debug.DrawRay(ray.origin, ray.direction * targetDistance, Color.green, 1.0f); bool canHit = false; if (!canHit) { MapCellData myCell = mapControl.GetData(transform.position); foreach (Vector3 otherPosition in myCell.ExtraFiringPositions) { ray.origin = otherPosition; canHit = !Physics.Raycast(ray, targetDistance, layerMask); if (canHit) { break; } } } if (canHit) { closestTarget = target; closestDistance = targetDistance; } } if (closestTarget) { UnitControl targetControl = closestTarget.GetComponent <UnitControl>(); if (!targetControl) { Debug.Log(closestTarget.name + " doesn't have a unitControl"); } return(targetControl); } else { Debug.Log(transform.name + "'s scan found noone"); return(null); } }