我们先看射线的相关文档。
public static bool RayCast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
public static bool Raycast(Ray ray, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Parameters 参数
origin | The starting point of the ray in world coordinates. 在世界坐标,射线的起始点。 |
direction | The direction of the ray. 射线的方向。 |
distance | The length of the ray. 射线的长度。 |
layermask | A Layer mask that is used to selectively ignore colliders when casting a ray. 投射射线,选择投射的层蒙版。 |
queryTriggerInteraction | Specifies whether this query should hit Triggers. |
ray | The starting point and direction of the ray. 射线的起点和方向 |
hitInfo | If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). hitInfo将包含碰到器碰撞的更多信息。 |
RaycastHit 射线投射碰撞信息
struct in UnityEngine
Description 描述
Structure used to get information back from a raycast.
该结构用来获取射线投射返回的信息。
See Also: Physics.Raycast, Physics:Physics.Linecast, Physics.RaycastAll.
Variables 变量
barycentricCoordinate | The barycentric coordinate of the triangle we hit. 碰到的三角形的重心坐标。 |
collider | The Collider that was hit. 碰到的碰撞器。 |
distance | The distance from the ray's origin to the impact point. 从射线的原点到触碰点的距离。 |
lightmapCoord | The uv lightmap coordinate at the impact point. 在触碰点的UV光照贴图的坐标。 |
normal | The normal of the surface the ray hit. 射线触碰表面的法线。 |
point | The impact point in world space where the ray hit the collider. 在世界坐标空间,射线碰到碰撞器的接触点。 |
rigidbody | The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null. 碰到的该碰撞器上的刚体。如果碰撞器上没有附加刚体,那么返回null。 |
textureCoord | The uv texture coordinate at the impact point. 在触碰点的UV纹理坐标。 |
textureCoord2 | The secondary uv texture coordinate at the impact point. 在接触点处的第二套UV纹理坐标。 |
transform | The Transform of the rigidbody or collider that was hit. 碰到的该刚体或碰撞器的变换。 |
triangleIndex | The index of the triangle that was hit. 碰到的三角形的索引。 |
Returns 返回
bool True when the ray intersects any collider, otherwise false.
当光线投射与任何碰撞器交叉时为真,否则为假。
缺省参数:1.layerMask参数缺省为DefaultRaycastLayers,
2.distance被缺省为正无穷 ,
3. queryTriggerInteraction 被缺省为QueryTriggerInteraction.UseGlobal
什么意思呢?我们来看
简单来说就是,枚举为Ignore时,所有含碰撞器的物体,都将被射线检测忽略。为UseGlobal时反之。
先来个最简单的Demo
由于我们是以空格键来发射射线,所以Debug.DrawLine(transform.position, hit.transform.position);画得线我截不到。见谅。
******************************************************************************************************************************************************
******************************************************************************************************************************************************
然后是layerMask。我们可以看到默认Layer为Default,我们可以左击选择Add Layer,来查看更详细的层级信息,同时也解释了为什么前面Demo的LayerMask.GetMask()为0。
暂时先写这么多,以后会补充。