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