判断是否有敌人的3种方法

  1. //1、只攻击正前方的单位,向前发射一条射线,攻击碰到的单位  
  2. RaycastHit hit;  
  3. //range 射线的长度,即攻击范围,maskTarget敌方单位的mask,只攻击敌方单位  
  4. if(Physics.Raycast(unit.thisT.position, unit.thisT.forward, out hit, range, maskTarget)){  
  5. Unit targetTemp=hit.collider.gameObject.GetComponent();  
  6. if(targetTemp!=null && targetTemp.HPAttribute.HP>0){  
  7. target=targetTemp;  
  8. if(attackMode==_AttackMode.StopNAttack){  
  9. if(attackMethod!=_AttackMethod.Melee) unit.StopAnimation();  
  10. unit.StopMoving();  
  11. }  
  12. }  
  13. }  
  14.   
  15. //2、以己方单位为圆心的某一半径长度内  
  16.   
  17. //返回相交球的所有碰撞体  
  18. Collider[] cols=Physics.OverlapSphere(unit.thisT.position, range, maskTarget);  
  19. //if(cols!=null && cols.Length>0) Debug.Log(cols[0]);  
  20. if(cols.Length>0){  
  21. Collider currentCollider=cols[Random.Range(0, cols.Length)];  
  22. Unit targetTemp=currentCollider.gameObject.GetComponent();  
  23. if(targetTemp!=null && targetTemp.HPAttribute.HP>0){  
  24. target=targetTemp;  
  25. if(attackMode==_AttackMode.StopNAttack){  
  26. if(attackMethod!=_AttackMethod.Melee) unit.StopAnimation();  
  27. unit.StopMoving();  
  28. }  
  29. }  
  30. }  
  31.   
  32. //3、以己方单位为圆心的扇形范围内  
  33. Collider[] cols=Physics.OverlapSphere(unit.thisT.position, range, maskTarget);  
  34. //if(cols!=null && cols.Length>0) Debug.Log(cols[0]);  
  35. if(cols.Length>0){  
  36. Collider currentCollider=cols[0];  
  37. foreach(Collider col in cols){  
  38. Quaternion targetRot=Quaternion.LookRotation(col.transform.position-unit.thisT.position);  
  39. if(Quaternion.Angle(targetRot, unit.thisT.rotation)  
  40. Unit targetTemp=currentCollider.gameObject.GetComponent();  
  41. if(targetTemp!=null && targetTemp.HPAttribute.HP>0){  
  42. target=targetTemp;  
  43. if(attackMode==_AttackMode.StopNAttack){  
  44. if(attackMethod!=_AttackMethod.Melee) unit.StopAnimation();  
  45. unit.StopMoving();  
  46. }  
  47. break;  
  48. }  
  49. }  
  50. }  
  51. }  
http://blog.csdn.net/fzhlee/article/details/8891711

你可能感兴趣的:(判断是否有敌人的3种方法)