物理引擎之 collider

碰撞發(fā)生的必要條件,雙方物體必須有一個(gè)是剛體,且必須都有Collider。 碰撞分為三種碰撞

  • 靜態(tài)碰撞 只有一個(gè)有剛體的碰撞,如果沒有剛體的物體去碰撞帶有剛體的物體時(shí),有的時(shí)候會(huì)檢測(cè)不到,因?yàn)閯傮w休眠的原因。
  • 動(dòng)態(tài)碰撞 兩個(gè)都有剛體的碰撞
  • 運(yùn)動(dòng)學(xué)碰撞 兩個(gè)都必須有剛體,而且有一個(gè)是運(yùn)動(dòng)學(xué)剛體,這時(shí)兩個(gè)物體碰撞的時(shí)候,運(yùn)動(dòng)學(xué)剛體不會(huì)受到影響。
Paste_Image.png
Paste_Image.png

Paste_Image.png
  • Bounds 這個(gè)是Collider的外框我們用代碼來把這個(gè)邊框給畫出來

    void Update()
     { 
      DrawBounds(GetComponent<Collider>().bounds);
      }
     void DrawBounds(Bounds bounds)
    {
      Vector3[] points = new Vector3[6];
      points[0] = bounds.min + Vector3.right * bounds.size.x;
      points[1] = bounds.min + Vector3.forward * bounds.size.z;
      points[2] = bounds.min + Vector3.up * bounds.size.y;
      points[3] = bounds.max - Vector3.right * bounds.size.x;
      points[4] = bounds.max - Vector3.forward * bounds.size.z;
      points[5] = bounds.max - Vector3.up * bounds.size.y;
    
      Debug.DrawLine(bounds.min, points[0], Color.cyan);
      Debug.DrawLine(bounds.min, points[1], Color.cyan);
      Debug.DrawLine(bounds.min, points[2], Color.cyan);
    
      Debug.DrawLine(bounds.max, points[3], Color.cyan);
      Debug.DrawLine(bounds.max, points[4], Color.cyan);
      Debug.DrawLine(bounds.max, points[5], Color.cyan);
      Debug.DrawLine(points[0], points[4], Color.cyan);
      Debug.DrawLine(points[1], points[5], Color.cyan);
      Debug.DrawLine(points[2], points[3], Color.cyan);
    
      Debug.DrawLine(points[0], points[5], Color.cyan);
      Debug.DrawLine(points[1], points[3], Color.cyan);
      Debug.DrawLine(points[2], points[4], Color.cyan);
    

    }

Paste_Image.png

當(dāng)我們旋轉(zhuǎn)他們的時(shí)候我們看下他們的外框變化

若水GIF截圖_2017年1月10日23點(diǎn)57分55秒.gif
  • contactOffset這個(gè)值和Edit=>ProjectSetting=>Physics中的這個(gè)面板上


    Paste_Image.png

    這個(gè)值是是當(dāng)他們的collider相差為多少距離的時(shí)候開始檢測(cè)碰撞(觸發(fā)器修改這個(gè)值無效)
    觸發(fā)信息檢測(cè):
    1.MonoBehaviour.OnTriggerEnter( Collider other )當(dāng)進(jìn)入觸發(fā)器
    2.MonoBehaviour.OnTriggerExit( Collider other )當(dāng)退出觸發(fā)器
    3.MonoBehaviour.OnTriggerStay( Collider other )當(dāng)逗留觸發(fā)器

碰撞信息檢測(cè):
1.MonoBehaviour.OnCollisionEnter( Collision collisionInfo ) 當(dāng)進(jìn)入碰撞器
2.MonoBehaviour.OnCollisionExit( Collision collisionInfo ) 當(dāng)退出碰撞器
3.MonoBehaviour.OnCollisionStay( Collision collisionInfo ) 當(dāng)逗留碰撞器
我們?cè)谡{(diào)用這種方法,尤其是Stay的時(shí)候要注意到剛體休眠的情況

Paste_Image.png
  • ClosetPointOnbounds 一個(gè)點(diǎn)距離Collider外框最近的一個(gè)點(diǎn),參數(shù)是空間的某個(gè)具體點(diǎn)
    這個(gè)我們寫個(gè)代碼測(cè)試下,也可以大致感覺到Collider的盒子
Paste_Image.png
Paste_Image.png

我們對(duì)Cube進(jìn)行旋轉(zhuǎn)

  • Raycast 這個(gè)是collider的射線檢測(cè),和我們常用的Physic不一樣。這個(gè)檢測(cè)是只有在射到自身(腳本所掛載的物體)的時(shí)候才會(huì)返回True
Paste_Image.png
若水GIF截圖_2017年1月10日23點(diǎn)25分33秒.gif

Collier的類型

  • Box Collier 盒子碰撞器
  • Sphere Collider 球形碰撞器
  • Capsule Collider 膠囊體碰撞器
  • Mesh Collider 網(wǎng)格碰撞器
Paste_Image.png

兩個(gè)紅圈里邊就是他們的網(wǎng)格,有些復(fù)雜的模型都會(huì)有簡(jiǎn)模提供,我們的這個(gè)MeshCollider最好也是用簡(jiǎn)模,減少計(jì)算量。還有一個(gè)比較重要的就是Convex(箭頭指的選項(xiàng))也一定要勾選,否則該碰撞器不會(huì)起到作用。

  • Terrain Collider 地形碰撞器
Paste_Image.png

箭頭指的就是我們地形上邊的樹是否使用碰撞器

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容