批量射线检测

public Transform ForeachArray(List npc, Camera cam)
{
//定义9条射线
Ray[] ray=new Ray[9];
//raycasthit
RaycastHit[] rayCasthit= new RaycastHit[9];
int k = 0;
//定义屏幕9个点
Vector3[,] vectorArray=new Vector3[3,3];
float widths=0;
float heights=0;
float width = Screen.width / 4f;
float height = Screen.height / 4f;
Debug.Log(“width” + width);
Debug.Log(“height” + height);
//生成9个点
for (int i = 0; i < 3; i++)
{
widths += width;
for (int j = 0; j < 3; j++)
{
heights += height;
vectorArray[i,j]=new Vector3(widths, heights,0);
}
}
//二维数组遍历
for (int i = 0; i < vectorArray.GetLength(0); i++)
{
for (int j = 0; j < vectorArray.GetLength(1); j++)
{
Debug.Log(“vectorArray[i,j]===”+vectorArray[i,j]);

        }
    }

    //存储9个raycasthit
    for (int i = 0; i <3 ; i++)
    {
        for (int j = 0; i < 3; i++)
        {
             ++k;
             ray[k]=cam.ScreenPointToRay(vectorArray[i,j]);
             Physics.Raycast(ray[k],out rayCasthit[k]);
        }
    }
//    List<GameObject> npclists = NPCManager.NPCManager_instance.GetNPC();
    if (ray!=null)
    {
        for (int i = 0; i < ray.Length-1; i++)
        {

            for (int j = 0; j < npc.Count; j++)
            {
                Debug.Log(npc.Count);
               // Debug.Log(rayCasthit[i].collider.gameObject.name);
                if (rayCasthit[i].collider!=null)
                {
                    Debug.Log("npc[j].gameObject.name" + npc[j].gameObject.name);
                    bool isSame = rayCasthit[i].collider.gameObject.name == npc[j].gameObject.name;
                    if (isSame)
                    {
                        Debug.Log("npc[j].gameObject.name" + npc[j].gameObject.name);
                        //判断距离
                        bool isDistance = Vector3.Distance(npc[j].gameObject.transform.position, rayCasthit[i].point) < 100f;
                        if (isDistance)
                        {
                            Debug.Log("npc[j].gameObject.name" + npc[j].gameObject.name);
                            Debug.Log(npc[j].gameObject.name);
                            return npc[j].gameObject.transform;
                        }
                    }
                }
                
            }              
        } 
    }
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值