笔记-<射线检测>

本文详细介绍了Unity3D中的射线检测技术,重点关注RaycastHit类的使用,如何创建并发射射线,以及如何获取射线碰撞信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ray (origin : Vector3, direction : Vector3) ;

上面的函数指:创建一个新的射线,开始于origin沿着direction的方向。

eg:

public Ray ray = new Ray(transform.position, transform.forward);

RaycastHit:

用来获取从raycast函数中得到的信息反馈的结构。
返回的信息包括:
point
在世界空间中,射线碰到碰撞器的碰撞点。

norma
射线所碰到的表面的法线。

barycentricCoordinate
所碰到的三角形的重心坐标。

distance
从光线的原点到碰撞点的距离。

triangleIndex
碰到的三角形的索引。

textureCoord
在碰撞点的UV纹理坐标。

textureCoord2
碰撞点的第二个UV纹理坐标。

lightmapCoord
所在碰撞点的光照图UV坐标。

collider
碰到的碰撞器。

rigidbody
碰到的碰撞器的Rigidbody。如果该碰撞器没有附加刚体那么它为null。

transform
碰到的刚体或碰撞器的变换。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
最后是检测,检测是一个BOOL类型:
bool isCollider=Physic.Raycast(ray,out hit,1000,LayerMask.GetMask("MapCube"));
上面的代码的意思是:以射线ray经过的1000长度之内,与第一个对象进行的物理碰撞的信息,储存在hit中,仅对MapCube层的对象进行检测。
如果有碰撞就返回一个ture,如果没有就返回一个false。

所以以上函数做了两件事:
1.检测是否碰撞;
2.返回了碰撞物体的信息;


  • The impact point in world space where the ray hit the collider.
    在世界空间中,射线碰到碰撞器的碰撞点。
  • The normal of the surface the ray hit.
    射线所碰到的表面的法线。
  • The barycentric coordinate of the triangle we hit.
    所碰到的三角形的重心坐标。
  • The distance from the ray's origin to the impact point.
    从光线的原点到碰撞点的距离。
  • The index of the triangle that was hit.
    碰到的三角形的索引。
  • The uv texture coordinate at the impact point.
    在碰撞点的UV纹理坐标。
  • The secondary uv texture coordinate at the impact point.
    碰撞点的第二个UV纹理坐标。
  • The uv lightmap coordinate at the impact point.
    所在碰撞点的光照图UV坐标。
  • The Collider that was hit.
    碰到的碰撞器。
  • The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
    碰到的碰撞器的Rigidbody。如果该碰撞器没有附加刚体那么它为null。
  • The Transform of the rigidbody or collider that was hit.
    碰到的刚体或碰撞器的变换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值