Three modes to determine whether there are enemies around

Source: Internet
Author: User

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;              }          }     }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.