福感科技有限公司 欢迎您!
联系方式

    地址:北京市平谷区马坊镇金河北街17号院3号楼7层712

    电话:010-89968230

    网站:http://www.fgsense.com

使用Unity3D的射线碰撞检测方法总结

2020-11-23 19:07:33      点击:

射线检测故名就是通过射线去检测是否和碰撞器产生了交集,和碰撞器与碰撞器发生交集一样,如果检测到了会返回一个真。
射线的用法很多:比如检测是否跳跃,通过向地面投射射线控制在地面时候可以跳起。
        射击游戏中可以通过定长射线去判断目标物体是否被击中等

主要用到的工具类有:

  • Physics
  • RaycastHit 光线投射碰撞
  • Ray 射线


第1种方法:Physics.Linecast 线性投射
从开始位置到结束位置做一个光线投射,如果与碰撞体交互,返回真。

 Debug.DrawLine(transform.position, Line_floor.position, Color.red, 1f);
 bool grounded  = Physics.Linecast(transform.position, Line_floor.position, 1 << LayerMask.NameToLayer("Ground"));
 if (grounded)
 {
     Debug.LogError("发生了碰撞");   
 }
 else {
     Debug.LogError("碰撞结束");
 }
第二种方法在场景中投下可与所有碰撞器碰撞的一条光线。可控制投射方向和投射长度。
Vector3 fwd = transform.TransformDirection(-Vector3.up);
bool grounded =  Physics.Raycast(transform.position,fwd, 10 );
if (grounded)
 {
   Debug.LogError("发生了碰撞");   
 }
else 
{
    Debug.LogError("碰撞结束");
 }
第三种方法:在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息。



RaycastHit hit;
bool grounded = Physics.Raycast(transform.position, -Vector3.up, out hit);
// 可控制投射距离bool grounded = Physics.Raycast(transform.position, -Vector3.up, out hit,100.0);
if (grounded)
{
Debug.LogError("发生了碰撞");
Debug.LogError("距离是:" + hit.distance);
Debug.LogError("被碰撞的物体是:" + hit.collider.gameObject.name);
}
else {
Debug.LogError("碰撞结束");
}
注意:这里返回的碰撞器的信息是依次的,先返回第一个碰撞的,第一个碰撞结束后才返回第二个。


第四种方法:Physics.RaycastAll 所有光线投射。

投射一条光线并返回所有碰撞,也就是投射光线并返回一个RaycastHit[]结构体。


RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, -Vector3.up, 100.0F);
int i = 0;
while (i < hits.Length)
{
Debug.LogError("发生了碰撞");
RaycastHit hit = hits[i];
Debug.LogError("被碰撞的物体是:" + hit.collider.gameObject.name);
i++;
}


第五种方法:控制碰撞的层,可以设置射线的长度,并且用debug查看射线的长度。

使用层的时候,要注意,要给别的对象也附上层的名字,不能用缺省,会出问题。


RaycastHit hit;
// Debug.DrawLine()
bool grounded = Physics.Raycast(transform.position, transform.up, out hit, 10000f, 1 << LayerMask.NameToLayer("Diren"));
Debug.DrawRay(transform.position, transform.up * 10000f, Color.red);
if (grounded)
{
Debug.LogError("发生了碰撞");
Debug.LogError("距离是:" + hit.distance);
Debug.LogError("被碰撞的物体是:" + hit.collider.gameObject.name);

}
else {
Debug.LogError("碰撞结束");
}
第五种:Physics.OverlapSphere 相交球。
返回球型半径之内(包括半径)的所有碰撞体 collider[]。可用于拾取物品用。此方法在VR交互时为了提高用户体验,使用较多。
Collider[] col =  Physics.OverlapSphere(transform.position,1f, 1 << LayerMask.NameToLayer("zhuangbei"));
if (col.Length > 0)
{
foreach (Collider zhuangbei in col)
{
  zhuangbei.gameObject.GetComponent().material.color = Color.red;
}
}

Copyright 2019 www.fgsense.com

福感科技有限公司 版权所有 All Rights Reserved

京ICP备20002031号

010-89968230