我們先看射線的相關文檔。
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
什麼意思呢?我們來看
Unity基礎篇:射線檢測Physics.Raycast相關功能整合。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX6FFVOdXSE1UMFpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zN3EjNwMjM4EDOwkDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
簡單來說就是,枚舉為Ignore時,所有含碰撞器的物體,都将被射線檢測忽略。為UseGlobal時反之。
先來個最簡單的Demo
由于我們是以空格鍵來發射射線,是以Debug.DrawLine(transform.position, hit.transform.position);畫得線我截不到。見諒。
******************************************************************************************************************************************************
******************************************************************************************************************************************************
然後是layerMask。我們可以看到預設Layer為Default,我們可以左擊選擇Add Layer,來檢視更詳細的層級資訊,同時也解釋了為什麼前面Demo的LayerMask.GetMask()為0。
暫時先寫這麼多,以後會補充。