码迷,mamicode.com
首页 > 其他好文 > 详细

判断周围是否有敌人的三种模式(转)

时间:2015-08-14 19:46:18      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

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


判断周围是否有敌人的三种模式(转)

标签:

原文地址:http://my.oschina.net/u/698044/blog/492523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!