LayerMask
描述
指定要在?Physics.Raycast?中使用的層。
代碼
[SerializeField] protected LayerMask whatIsGround;
Physics2D.Raycast
函數(shù)結(jié)構(gòu)
public static?RaycastHit2D?Raycast?
(Vector2?origin,
?Vector2?direction,
float?distance= Mathf.Infinity,
int?layerMask= DefaultRaycastLayers,
float?minDepth= -Mathf.Infinity, float?maxDepth= Mathf.Infinity);
參數(shù)
origin | 射線在 2D 空間中的起點(diǎn)。 |
direction | 表示射線方向的矢量。 |
distance | 射線的最大投射距離。 |
layerMask | 過濾器,用于僅在特定層上檢測(cè)碰撞體。 |
minDepth | 僅包括 Z 坐標(biāo)(深度)大于或等于該值的對(duì)象。 |
maxDepth | 僅包括 Z 坐標(biāo)(深度)小于或等于該值的對(duì)象。 |
代碼
public virtual bool IsGroundDetected() => Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);
Raycast函數(shù)返回的bool值代表了他是否接觸地面。
groundCheck.position
groundCheck.position是通過transform定義的,在使用過程中,我們將在物體下方
創(chuàng)建新的子物體來代表GroundCheck
通過拖動(dòng)至
的方式。
Gizmos.DrawLine
函數(shù)結(jié)構(gòu)
public static void?DrawLine?(Vector3?from,?Vector3?to);
描述
繪制一條從 from 開始到 to 的線。
代碼
Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));
主要用于將RayCast的范圍可視化如圖中白線。
//---------------------------------------------//
注:本人也是萌新一個(gè),邊學(xué)邊記,寫下來主要是怕搞忘了。文章如有問題,還請(qǐng)多多包涵。
本文章在本人學(xué)完之后將會(huì)進(jìn)一步進(jìn)行優(yōu)化,可能的話會(huì)以視頻的形式進(jìn)行講解,如有需要可以先收藏。
文章參考資料:Unity自帶的手冊(cè)文章來源:http://www.zghlxwxcb.cn/news/detail-770568.html
Unity 腳本 API文章來源地址http://www.zghlxwxcb.cn/news/detail-770568.html
到了這里,關(guān)于Unity常用方法-- Collision碰撞檢測(cè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!