//method to search for a target bool SearchForTargetToAttack() { //Search if there are enemy units in range: bool Found = false; float Size = SearchRange; Vector3 SearchFrom = transform.position; //only for NPC factions: //if there's no city center to protect: if (AttackRangeCenter == null) { AttackRangeFromCenter = false; //we're not defending any city center then: } //if there's a city center to protect if (AttackRangeFromCenter == true && AttackRangeCenter != null) { SearchFrom = AttackRangeCenter.transform.position; //the search pos is the city center Size = AttackRangeCenter.GetComponent <Border>().Size; //and the search size is the whole city border size: } Collider[] ObjsInRange = Physics.OverlapSphere(SearchFrom, Size); int i = 0; //counter while (i < ObjsInRange.Length && Found == false) { Unit UnitInRange = ObjsInRange[i].gameObject.GetComponent <Unit>(); if (UnitInRange && UnitInRange != UnitMgr) //make sure that there's a unit in range and that unit is not this one. { //If it's a unit object if (UnitInRange.enabled == true) { //if the unit comp is enabled //If this unit and the target have different teams and make sure it's not dead. if (CanAttackFaction(UnitInRange.FactionID) && UnitInRange.Dead == false) { //if the unit is visible: if (UnitInRange.IsInvisible == false) { if (AttackTarget == null) { if (AttackMgr.CanAttackTarget(this, ObjsInRange[i].gameObject)) { //if the unit can attack the target. //Set this unit as the target //if this is a unit: if (AttackerType == AttackerTypes.Unit) { MvtMgr.LaunchAttack(UnitMgr, ObjsInRange[i].gameObject, MovementManager.AttackModes.Assigned); } else //if this is a building { SetAttackTarget(ObjsInRange[i].gameObject); } Found = true; } } } } } } i++; } return(Found); }
//method to search for a target bool SearchForTargetToAttack() { if (AttackTarget != null) { return(true); } //Search if there are enemy units in range: bool Found = false; float Size = SearchRange; Vector3 SearchFrom = transform.position; //only for NPC factions: Collider[] ObjsInRange = Physics.OverlapSphere(SearchFrom, Size); print(ObjsInRange); int i = 0; //counter if (AttackTarget == null) { while (i < ObjsInRange.Length && Found == false) { Unit UnitInRange = ObjsInRange[i].gameObject.GetComponent <Unit>(); stats RpgPlayer = ObjsInRange[i].gameObject.GetComponent <stats>(); if (UnitInRange && UnitInRange != UnitMgr) //make sure that there's a unit in range and that unit is not this one. { //If it's a unit object if (UnitInRange.enabled == true) { //if the unit comp is enabled //If this unit and the target have different teams and make sure it's not dead. if (CanAttackFaction(UnitInRange.FactionID) && UnitInRange.Dead == false) { //if the unit is visible: if (UnitInRange.IsInvisible == false) { if (AttackTarget == null) { if (AttackMgr.CanAttackTarget(this, ObjsInRange[i].gameObject, AttackExceptionList)) { //if the unit can attack the target. //Set this unit as the target //if this is a unit: if (AttackerType == AttackerTypes.Unit) { if (Vector3.Distance(transform.position, ObjsInRange[i].gameObject.transform.position) > Size) { return(false); } Debug.Log("hello"); MvtMgr.LaunchAttack(UnitMgr, ObjsInRange[i].gameObject, MovementManager.AttackModes.Assigned); } else //if this is a building { SetAttackTarget(ObjsInRange[i].gameObject); } Found = true; } } } } } } else if (RpgPlayer) { if (AttackTarget == null) { if (AttackMgr.CanAttackTarget(this, ObjsInRange[i].gameObject, AttackExceptionList)) { if (AttackerType == AttackerTypes.Unit) { Debug.Log("hello"); MvtMgr.LaunchAttack(UnitMgr, ObjsInRange[i].gameObject, MovementManager.AttackModes.Assigned); } else //if this is a building { SetAttackTarget(ObjsInRange[i].gameObject); } Found = true; } } } i++; } } return(Found); }