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
碰到的刚体或碰撞器的变换。
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.
碰到的刚体或碰撞器的变换。