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

转自:http://www.taidous.com/thread-43595-1-1.html


其他论坛看到的,现在分享给大家

//1、只攻击正前方的单位,向前发射一条射线,攻击碰到的单位

RaycastHit hit;

//range 射线的长度,即攻击范围,maskTarget敌方单位的mask,只攻击敌方单位

if(Physics.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();

}

}

}

//2、以己方单位为圆心的某一半径长度内

//返回相交球的所有碰撞体

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();

}

}

}

//3、以己方单位为圆心的扇形范围内

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;

}

}

}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值