Only attack the unit that is in front of the attack, and launch a ray forward.
Raycasthit hit; // The length of the range Ray, that is, the attack range. The masktarget is the mask of the enemy's unit. Only the IF (physics) of the enemy's unit is attacked. raycast (unit. thist. position, unit. thist. forward, out hit, range, masktarget) {unit targettemp = hit. collider. gameobject. getcomponent (); If (targettemp! = NULL & targettemp. hpattribute. HP> 0) {target = targettemp; If (attackmode = _ attackmode. stopnattack) {If (attackmethod! = _ Attackmethod. Melee) unit. stopanimation (); Unit. stopmoving ();}}}
Within a certain radius of the center in units of oneself
// Return all collision bodies of the intersection balls collider [] Cols = physics. overlapsphere (unit. Thist. Position, range, masktarget); // If (Cols! = NULL & cols. length> 0) debug. log (Cols [0]); If (cols. length> 0) {collider currentcollider = Cols [random. range (0, cols. length)]; Unit targettemp = currentcollider. gameobject. getcomponent (); If (targettemp! = NULL & targettemp. hpattribute. HP> 0) {target = targettemp; If (attackmode = _ attackmode. stopnattack) {If (attackmethod! = _ Attackmethod. Melee) unit. stopanimation (); Unit. stopmoving ();}}}
Within the range of slices centered on one's own unit
Collider[] cols=Physics.OverlapSphere(unit.thisT.position, range, maskTarget);//if(cols!=null && cols.Length>0) Debug.Log(cols[0]);if(cols.Length>0){ Collider currentCollider=cols[0]; foreach(Collider col in cols){ Quaternion targetRot=Quaternion.LookRotation(col.transform.position-unit.thisT.position); if(Quaternion.Angle(targetRot, unit.thisT.rotation) Unit targetTemp=currentCollider.gameObject.GetComponent(); if(targetTemp!=null && targetTemp.HPAttribute.HP>0){ target=targetTemp; if(attackMode==_AttackMode.StopNAttack){ if(attackMethod!=_AttackMethod.Melee) unit.StopAnimation(); unit.StopMoving(); } break; } } }}