Placeholder Image

字幕列表 影片播放

  • In order to make a collider into a

    要讓碰撞體成為觸發器(Trigger) 只要從檢視勾選碰撞體的Is Trigger屬性即可

  • trigger we simply check the

    當碰撞體變成觸發器後就不再有碰撞行為 必須用程式來偵測判斷

  • 'Is Trigger' checkbox on the component

    如同一般的碰撞體

  • settings in the inspector.

    當其他碰撞體進入觸發器時 會呼叫觸發事件

  • When a collider is a trigger things will

    在這個範例裡我們設定方形碰撞體為觸發器 並放一個會掉落的球體物件

  • no longer bump into it. Instead they will

    這個球體物件會掉進觸發器內 然後向前滾動最後離開

  • pass through it and this can be detected

    來看看觸發器上的程式腳本

  • via code.

    有觸發進入(OnTriggerEnter)

  • Similar to normal colliders,

    並寫入訊息到控制台

  • events are called when other colliders

    以及觸發持續(OnTriggerStay)

  • are overlapping with the trigger collider.

    和觸發結束(OnTriggerExit)

  • In this example we have a box collider

    如果我們將遊戲暫停 並單步執行每一幀

  • with Is Trigger checked

    可以看到物件進入觸發器時 控制台顯示物件進入的訊息

  • and a falling 'prop samoflange' ball object.

    持續停留的幀數和最後離開的訊息

  • This object enters the trigger,

    如同碰撞體觸發的規則一樣 兩個物件其中一個必須有剛體

  • stays as it rolls forward and then exits.

    設定觸發器的標準做法 是將觸發器設定為靜態物件

  • To check these events we attach a script

    這樣它們就不會被物理引擎所影響

  • to the trigger, which is registering

    所以通常是如同剛剛的範例 用一個有剛體的物件穿過它

  • OnTriggerEnter and logging to the console

    我們讓有剛體的物件穿過觸發範圍 然後進行觸發事件檢查

  • and likewise OnTriggerStay

    因此用來作為觸發器的碰撞體 也可以稱作觸發範圍(Trigger Zone)

  • and OnTriggerExit.

    如此一來你就可以由程式來進行觸發判斷 而不需要由物件產生實際的物理碰撞

  • So if we pause the game

    舉例來說, 這是一個飄浮墊

  • and step through one frame at a time,

    我們可以讓球體物件往上增加一個力道(Force) 讓停留在觸發範圍內的球有飄浮的效果

  • looking at the console will show that the

    我們可以呼叫觸發範圍內的物件

  • object enters the trigger

    因為在觸發範圍裡的物件 會被存在一個名為other碰撞體類別

  • stays for a number of frames

    然後我們可以呼叫它的剛體 並往上增加一個力道

  • and eventually exits the trigger.

    Vector3.up是世界座標往上的簡寫

  • In the same way as standard collisions,

    然後乘上一個值,並將結果存成一個變數

  • one of the two objects must have a rigidbody.

    最後設定力道的模式函式就完成了

  • It's standard practice to make sure that your trigger

    現在我們將球移動到飄浮墊上

  • colliders are static objects,

    為了避免它亂跑我們凍結X和Z軸移動

  • meaning they will not be moved by the

    只有Y軸允許被改變 因此它就可以產生漂浮的效果

  • physics engine. So usually you will make

    接下來播放遊戲 可以看到球停留在漂浮的範圍內

  • a trigger and then pass a rigidbody

    而球體就可以在沒有任何物理運算下 形成了漂浮的效果

  • through it. This example is no different.

  • We're passing our rigidbody object

  • through the trigger zone and detecting

  • when something enters that trigger.

  • So the intention with the trigger collider,

  • also known as a trigger zone,

  • is that you can call code without the

  • objects in your game physically colliding.

  • So for example, with our hoverpad,

  • we can add a force to our samoflange ball

  • to give the effect of it hovering

  • so long as it stays within the trigger zone.

  • For example, we could address the object

  • that's currently staying within the trigger

  • because the object that's staying

  • is saved in a variable called 'other'

  • of type Collider. We could then address

  • the rigidbody and add a force

  • in the direction of Vector3.up,

  • a shortcut for 'up' in world coordinates.

  • We could then multiply by a number

  • which we'll save as a variable.

  • And we'll finish out the AddForce

  • by choosing acceleration as the mode.

  • Now we'll move our object over the hoverpad

  • and we'll use Freeze Position to keep it

  • in the same place in X and Z.

  • Allowing the force of Y to create

  • the hovering effect.

  • And now if I press play, as you can see

  • as long as it stays within that area

  • it creates the effect of hovering without

  • having to interact without any part

  • of the geometry.

In order to make a collider into a

要讓碰撞體成為觸發器(Trigger) 只要從檢視勾選碰撞體的Is Trigger屬性即可

字幕與單字

影片操作 你可以在這邊進行「影片」的調整,以及「字幕」的顯示

B1 中級 中文 英國腔 觸發器 物件 範圍 球體 呼叫 物理

Colliders作為觸發器 - Unity官方教程 (Colliders as Triggers - Unity Official Tutorials)

  • 37 2
    朱瑛 發佈於 2021 年 01 月 14 日
影片單字