赞
踩
1.普通射线检测(一般用于检测某一个物体)
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin ,ray.direction , Color.red);
RaycastHit hit;
if(Physics .Raycast (ray,out hit,int.MaxValue,1<
{
Debug.Log("检测到物体");
}
2.直线射线检测多个物体
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin ,ray.direction , Color.red);
RaycastHit[] hit = Physics.RaycastAll(ray, Mathf.Infinity, 1 << LayerMask.NameToLayer("layername"));
if(hit .Length >0)
{
for (int i = 0; i < hit.Length ; i++)
{
Debug.Log("检测到物体"+hit[i].collider.name );
}
}
3.球形射线检测(一般用于检测周边物体)
int radius = 3;
Collider[] cols = Physics.OverlapSphere(this.transform.position, radius, Layer
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。