字幕列表 影片播放
Colliders are a component that allows
碰撞體(Collider)是遊戲物件的元件之一
the game object they're attached to
它可以讓所依附的遊戲物件 對其它碰撞體產生碰撞行為
to react to other colliders
而那些遊戲物件必須有剛體(Rigidbody)元件
provided that one of the game objects
碰撞體有不同的形狀與類別
has a rigidbody component attached.
在場景視圖內它們會以綠色外框形狀顯示
Colliders come in various shapes and types,
它們有這些主要的形狀 球形, 柱形和方形
and are denoted in the scene view
如果需要更複雜的形狀有兩個方法
by a green outline.
你可以用多個幾何物件 將它們的碰撞體放入不同的物件
They can have the following primitive shapes:
最後在階層裡結合成為新的碰撞結構
a sphere, a capsule and a box.
舉例來說, 這個工作檯由許多不同的 碰撞體放在各部位組成一個碰撞結構
For more complex shapes you have two options
另外一個方式是使用網格碰撞體
You can either combine several of these
網格碰撞體會和網格的形狀相同
primitive shapes together by applying
右邊的這個工作檯並沒有碰撞體 因為它使用了網格碰撞體
primitive colliders to different objects.
我們不建議使用網格碰撞體 因為網格碰撞體是依照網格形狀所產生
in our hierarchy.
所以可能會造成太複雜的碰撞結構 進而影響遊戲的效能
For example this workbench
這也是為甚麼通常 我們會使用組合式碰撞體的原因
has a number of objects which simply
此外還有第三種方式 來產生幾何形狀碰撞體
serve to make up it's different colliders
先簡化物件的模型 然後將模型製作成網格碰撞體即可
for various areas.
在這個範例裡我們所用的是 一個複雜的機器手臂
The other option is to use a mesh collider,
它擁有很複雜的網格 但我們並不希望它的網格碰撞體如此複雜
which will fit the exact shape of the
所以我們建立了第二組簡化的網格模型 然後用這些簡化的模型製作網格碰撞體
mesh that you specify.
像是這個機器爪的網格 比我們為它產生的網格碰撞體還要複雜
The workbench on the right has no hierarchy
這是用兩個FBX檔來完成
but instead uses a mesh collider.
一個是原始的機器手臂 和簡化過的幾何模型
The reason not to use a mesh collider
然後我們在每一個子物件裡 都加入了網格碰撞體
is that it will fit the exact shape
然後將簡化過的幾何模型 拖曳到對應的網格碰撞體的網格(Mesh)
of the mesh that you specify. So if you
這樣一來,我們就可以達到碰撞的需求 同時也避免效能的問題
only specify the mesh of your detailed model
當碰撞在遊戲引擎裡發生時 碰撞體會呼叫OnCollisionEnter事件
then it may be providing too detailed
在這個場景我們的球體物件 附有一個球形碰撞體和剛體
a collision mesh and effecting performance.
剛體有重量(Mass)和重力(Gravity)屬性
This is the reason why it's often better
當我播放時, 球體物件會掉落 並碰到其他物件
to make a compound setup instead.
我們的方體物件附有一個方形碰撞體
However it should be noted that a third
而在掉落的球體上我們加入了這個程式腳本
option for creating collision geometry
這個程式腳本進行了三個碰撞事件的檢查
is to use a separate, simpler set of
碰撞進入(OnCollisionEnter)
geometry and still use a mesh collider.
碰撞持續(OnCollisionStay)
In this example we have this complex
和碰撞結束(OnCollisionExit)
robot arm asset.
當這些事件被呼叫時我用Debug.Log語法 在控制台(Console)寫入資訊
It's a very detailed mesh
提示進入、停留和離開的訊息
but we don't want a mesh as complex
所以當我們檢視控制台內容時
as this for the collisions.
你會先看到碰撞進入 接下來碰撞持續了一陣子, 最後碰撞結束
So what we've done is built
如果我們將遊戲暫停 我們可以詳細的檢視這個範例
a secondary set of geometry which we've
當我單步執行每一幀
then applied to a series of mesh colliders.
你可以看到當碰撞開始時 控制台顯示了碰撞進入的訊息
For example this part of the claw is more
代表OnCollisionEnter事件剛被呼叫
detailed than the collision mesh
當我繼續後換OnCollisionStay被呼叫
that we've created for it.
控制台顯示了碰撞持續的訊息 因為兩個碰撞體還保持在碰撞的階段
And this has been done in two separate
直到兩個碰撞體不再有碰撞行為 OnCollisionExit事件就會被呼叫
FBX files -The original artwork and
要注意的是,如果要觸發碰撞(OnCollision) 兩個碰撞的物件之中一個必須有剛體元件
a simplified set of geometry.
We've then gone through each one,
applied a mesh collider and dragged
these meshes over as the
collision meshes to use.
This means that we get the kind of accuracy
that we need in terms of collision
without the performance overhead.
When collisions occur in the game engine
one collider strikes another and an event
called OnCollisionEnter is called.
In this scene our 'prop samoflange' object
has a sphere collider component and a rigidbody component.
The rigidbody provides mass and gravity.
When I play the game, one falls down
and strikes the other.
The power cube has a box collider attached to it.
Also attached to our falling object
is this script.
This script checks for three collision events.
OnCollisionEnter, OnCollisionStay
and OnCollisionExit.
When each of these occurs it writes
to the console using Debug.Log.
It will register when Enter is called,
when Stay is occurring
and when Exit is called.
So if we look at our console you can see
that Enter is called, Stay has occurred
for a while and then Exit is called.
If we pause the game and play we can look
at this example slowly.
As I step through the frames
when the collision occurs you can see
that Enter is called, so OnCollisionEnter
has just occurred.
As I continue OnCollisionStay is occurring.
You can see on the right here that it's
happening several times because these
two colliders are still in contact.
As we continue to step through
eventually OnCollisionExit is called
when the two colliders are no longer
in contact.
Note that for an OnCollision message to
to be sent, one of the two objects colliding
must have a rigidbody component.