unity中的射线检测

unity的射线检测

RaycastHit hit;//存储射线检测后的信息

if (Physics.Raycast(transform.position + Vector3.up * 0.3f, transform.forward, out hit, 5f))//进行射线检测 返回值为bool值,第一个参数:射线起始位置;第二个参数:射线的方向(transform.forward为脚本物体的z轴方向);第三个参数:将检测信息存储进hit中,当然 还有maxdistance:射线的最大长度;第五个参数:int  layermask:射线检测的图层。
{
if (hit.collider.tag == "Obstacle" && speed >= 3&&anim.GetCurrentAnimatorStateInfo(0).IsName("locamation"))
//RaycastHit常用的一些方法
{

Vector3 point = hit.point;

point.y = hit.collider.transform.position.y + hit.collider.bounds.size.y;
matchtarget = point;

}


}

}

 

//射线检测的灵活运用

if (Input.GetMouseButtonDown(0)) {

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//射线检测和摄像机鼠标配合使用
            if (Physics.Raycast(ray, out hit))
                if (hit.collider != null)
                    hit.collider.enabled = false;

 }

 

posted @ 2017-11-12 10:26  地球手术师  阅读(3146)  评论(0编辑  收藏  举报