創作內容

2 GP

Unity collider and trigger 原文翻譯

作者:Jomo│2016-08-15 02:52:02│巴幣:4│人氣:1628

傅老師9月開始要被華夏科大抓去教unity了,所以先來翻一篇吧!


Rigidbodies

剛體
A Rigidbody isthe main component that enables physical behaviour for an object.With a Rigidbody attached, the object will immediately respond togravity. If one or more Collider componentsare also added then the object will be moved by incoming collisions.
剛體是使能物件物理特性的主要組件。物件若帶剛體,則立即會對地心引力做出反應。若有一至多個碰撞器綁在物件上,則物件亦會對入撞產生反應。
Sincea Rigidbody component takes over the movement of the object it isattached to, you shouldn’t try to move it from a script by changingthe Transform propertiessuch as position and rotation. Instead, you should apply forces topush the object and let the physics engine calculate the results.
由於剛體特性會主導物件的物理性質移動,使用者不應再使用Transform屬性去修改物件的位置與角度;相反地,使用者應該使用物理力學的施力概念去推動物件,讓整個物件的所有物理性質由物理引擎去計算。
Thereare some cases where you might want an object to have a Rigidbodywithout having its motion controlled by the physics engine. Forexample, you may want to control your character directly from scriptcode but still allow it to be detected by triggers(see Triggers below).This kind of non-physical motion produced from a script is knownas kinematic motion.The Rigidbody component has a property called IsKinematic whichwill remove it from the control of the physics engine and allow it tobe moved kinematically from a script. It is possible to change thevalue of IsKinematic froma script to allow physics to be switched on and off for an object,but this comes with a performance overhead and should be usedsparingly.
但有些時候,你會希望物件有剛體特性,但卻不希望他的移動完全任由物理引擎去操縱。例如你將剛體用在主角身上,打算直接使用程式去操控他,而且想讓他仍能使用trigger來偵測碰撞(稍後會看到trigger是甚麼)以上所述的"直接使用程式操控",就是所謂的kinematic移動(編按:這種移動是透過玩家操作而介入的鬼魂式飄動,不是引擎能"預料"到、計算到)。剛體參數設置中有一個IsKinematic參數,就是用來忽略物理引擎操控,改為程式操控。IsKinematic設置也可以由程式開啟/關閉,但這會造成額外效能損耗,應少用。
Seethe Rigidbody and Rigidbody2D referencepages for further details about the settings and scripting optionsfor these components.

Sleeping

休眠
Oncea rigidbody is moving at less than a certain minimum linear orrotational speed, the physics engine will assume it has come to ahalt. When this happens, the object will not move again until itreceives a collision or force and so it will be set to “sleeping”mode. This optimisation means that no processor time will be spentupdating the rigidbody until the next time it is “awoken” (ie,set in motion again). For most purposes, the sleeping and waking ofrigidbodies happens transparently. However, an object might fail towake up if a static collider (ie, one without a rigidbody) is movedinto it or away from it by modifying the transform position. Thismight result, say, in the rigidbody object hanging in the air whenthe floor has been moved out from beneath it. In cases like this, theobject can be woken explicitly using the WakeUp function.See the Rigidbody and Rigidbody2Dcomponentpages for more information about sleeping.
當一個剛體的移動速度或旋轉速度低於某微量值時,物理引擎會認為他已經停止下來了。此時物體將被設成休眠狀態並且不再移動,直到有新的碰撞或是施力發生他才會甦醒過來。(編按:這樣做是因為---如此細微的變動實在不需要再花CPU時間去運算,辛辛苦苦去算一堆小數點,算了玩家也看不出來)大部分的情況下這個<休眠-甦醒>的動作,使用者是察覺不到也不須擔心的,但是!如果撞上受程式操控的鬼魂式移動的靜態碰撞器(編按:鬼魂到連剛體都沒綁!想出現在哪,程式就讓你違反物理公式出現在那兒),這種情況會讓前述剛體的<休眠-甦醒>失效。具體舉個例,你可能會看過<地板已經垮掉了但上面的剛體還飛在空中>這種情況。為了避免類似的事情,你可以透過程式的WakeUp函數明確地讓物件醒過來。


Colliders

碰撞器
Collider componentsdefine the shape of an object for the purposes of physicalcollisions. A collider, which is invisible, need not be the exactsame shape as the object’s mesh and in fact, a rough approximationis often more efficient and indistinguishable in gameplay.
碰撞器組件用來定義物體的物理碰撞體型。碰撞器通常設為透明,而且形狀會較為粗略,粗略的原因則是為了效能與可玩性。
Thesimplest (and least processor-intensive) colliders are theso-called primitive collidertypes. In 3D, these are the BoxCollider,SphereCollider and CapsuleCollider.In 2D, you can use the BoxCollider 2D and CircleCollider 2D.Any number of these can be added to a single object tocreate compoundcolliders.
最簡單的碰撞器稱為原生碰撞器。在3D下指的是BoxColliderSphereCollider以及CapsuleCollider;在2D下則是BoxCollider 2D以及CircleCollider 2D。你可用數個原生碰撞器綁在同一個物件上,藉以拼裝出一個稍複雜的碰撞結構(編按:這是個拼裝車的概念。。)。
Withcareful positioning and sizing, compound colliders can oftenapproximate the shape of an object quite well while keeping a lowprocessor overhead. Further flexibility can be gained by havingadditional colliders on child objects (eg, boxes can be rotatedrelative to the local axes of the parent object). When creating acompound collider like this, there should only be one Rigidbodycomponent, placed on the root object in the hierarchy.
可別小看前述所提的(拼裝車)組合式碰撞結構,透過一些巧思他可以既簡單又高效。若將碰撞器間使用親子關係設置,甚至可以組出非常靈活的碰撞結構。但這樣做的時候要特別注意---<剛體只能綁在最原頭的那個根物件上>。
Note,that primitive colliders will not work correctly with sheartransforms - that means that if you use a combination of rotationsand non-uniform scales in the tranform hierarchy so that theresulting shape would no longer match a primitive shape, theprimitive collider will not be able to represent it correctly.
請注意,原生碰撞器不能不均勻地放大尺寸。(編按:這道理太直覺了,不多翻譯了)
Thereare some cases, however, where even compound colliders are notaccurate enough. In 3D, you can use MeshColliders tomatch the shape of the object’s mesh exactly. In 2D, the PolygonCollider 2D willgenerally not match the shape of the sprite graphic perfectly but youcan refine the shape to any level of detail you like. These collidersare much more processor-intensive than primitive types, however, souse them sparingly to maintain good performance. Also, a meshcollider will normally be unable to collide with another meshcollider (ie, nothing will happen when they make contact). You canget around this in some cases by marking the mesh collideras Convex inthe inspector. This will generate the collider shape as a “convexhull” which is like the original mesh but with any undercuts filledin. The benefit of this is that a convex mesh collider can collidewith other mesh colliders so you may be able to use this feature whenyou have a moving character with a suitable shape. However, a goodgeneral rule is to use mesh colliders for scene geometry andapproximate the shape of moving objects using compound primitivecolliders.
當組合式碰撞結構還不能滿足你時,在3D的情況下考慮用MeshColliders來精準地描述出物件模型的實際體型;在2D情況下你可考慮用PolygonCollider2D(有待你的精心調整才能完美地符合Sprite體型)。請注意,這些碰撞器會比原生的更耗能,請自行慎用。而且,2MeshCollider互撞時可能會失誤!你需要將MeshCollider 設為Convex才能避免,這設置會幫你將碰撞器凹陷的地方補平。通常,MeshCollider用在場景物件;組合式碰撞結構用在移動物件。
Colliderscan be added to an object without a Rigidbody component to createfloors, walls and other motionless elements of a scene. These arereferred to as static colliders.In general, you should not reposition static colliders by changingthe Transform position since this will impact heavily on theperformance of the physics engine. Colliders on an objectthat does havea Rigidbody are known as dynamic colliders.Static colliders can interact with dynamic colliders but since theydon’t have a Rigidbody, they will not move in response tocollisions.
Thereference pages for the various collider types linked above havefurther information about their properties and uses.
碰撞器可以用在不帶剛體的物品,如地板、牆、以及其他場景中不動的物品,我們稱這種用法為<staticcollider靜態碰撞器>。一般來說,你不該透過程式去調變Transform來移動前述物件(鬼魂式的飄動),這會造成物理引擎效能上的極大負擔。相對的,當碰撞器用在帶有剛體的物品時,我們稱這種用法為<dynamiccollider動態碰撞器>。靜態碰撞器與動態碰撞器之間可以互動,可測到相互碰撞(Collision),只不過撞了也不會有反應(編按:因為其中一個沒帶剛體啊!!)

Physicsmaterials

物理質料
Whencolliders interact, their surfaces need to simulate the properties ofthe material they are supposed to represent. For example, a sheet ofice will be slippery while a rubber ball will offer a lot of frictionand be very bouncy. Although the shape of colliders is not deformedduring collisions, their friction and bounce can be configuredusing PhysicsMaterials.Getting the parameters just right can involve a bit of trial anderror but an ice material, for example will have zero (or very low)friction and a rubber material with have high friction andnear-perfect bounciness. See the reference pages for PhysicMaterial and PhysicsMaterial 2D forfurther details on the available parameters. Note that for historicalreasons, the 3D asset is actually calledPhysicMaterial (without theS) but the 2D equivalent is called PhysicsMaterial 2D (with theS).
當碰撞器互撞時,他們的表面需要進行物理質料的模擬。舉例來說:一片冰塊應該要很滑,而一塊橡皮則應既有摩擦力、又有彈性。物體的摩擦、彈性,應透過物理質料Physicalmaterials來設置。設置的過程會需要一些猜想與試誤法(編按:摩擦跟彈性應可憑你這輩子對事物的經驗去猜想)。基於本引擎開發的歷史因素,3D上要使用PhysicMaterial2D上要使用PhysicsMaterial 2D(注意有個s)

Triggers

觸發
Thescripting system can detect when collisions occur and initiateactions using the OnCollisionEnter function.However, you can also use the physics engine simply to detect whenone collider enters the space of another without creating acollision. A collider configured as a Trigger (usingthe IsTrigger property)does not behave as a solid object and will simply allow othercolliders to pass through. When a collider enters its space, atrigger will call the OnTriggerEnter functionon the trigger object’s scripts.
程式可以透過OnCollisionEnter偵測到碰撞產生,然而你也可以在不產生碰撞的情況下,用物理引擎直接偵測碰撞器是否有重疊。設為觸發Trigger的碰撞器(透過參數IsTrigger)不會表現地像固體,而是會可以穿透的。當有其他的碰撞器入撞時,帶有Trigger碰撞器的物件會呼叫OnTriggerEnter函數。

Scriptactions taken on collision

碰撞發生時程式的影響
Whencollisions occur, the physics engine calls functions with specificnames on any scripts attached to the objects involved. You can placeany code you like in these functions to respond to the collisionevent. For example, you might play a crash sound effect when a carbumps into an obstacle.
當碰撞發生時,物理引擎會呼叫碰撞物所攜程式內的某幾則預設函數,你可以在對應的函數內寫入想要的程式。舉例來說,可以在車子撞到障礙時用程式撥個撞碎的聲音。
Onthe first physics update where the collision is detected,the OnCollisionEnter functionis called. During updates where contact ismaintained, OnCollisionStay iscalled and finally, OnCollisionExit indicatesthat contact has been broken. Trigger colliders call theanalogous OnTriggerEnter, OnTriggerStay and OnTriggerExit functions.Note that for 2D physics, there are equivalent functionswith 2D appendedto the name, eg, OnCollisionEnter2D.Full details of these functions and code samples can be found on theScript Reference page for the MonoBehaviour class.
引擎呼叫函數的順序如下:首先碰撞被偵測到,OnCollisionEnter被呼叫;當兩物持續接觸未分離時,OnCollisionStay會被呼叫;最後當物件分開時,OnCollisionExit會被呼叫。Trigger也是類似的,分為OnTriggerEnterOnTriggerStayOnTriggerExit2D版本就是在後面加個2D尾綴即可。
Withnormal, non-trigger collisions, there is an additional detail that atleast one of the objects involved must have a non-kinematic Rigidbody(ie, IsKinematic mustbe switched off). If both objects are kinematic Rigidbodiesthen OnCollisionEnter,etc, will not be called. With trigger collisions, this restrictiondoesn’t apply and so both kinematic and non-kinematic Rigidbodieswill prompt a call to OnTriggerEnter whenthey enter a trigger collider.
在一般的、非觸發的碰撞發生時,額外有條規則要注意的是:至少一個碰撞方要是<非Kinematic>的剛體(就是Kinematic要有一個是關的)。如果雙方都是Kinematic+剛體,則碰撞不會發生!好在,雙方都是KinematicTrigger會發生(編按:甚好~)

Colliderinteractions

Collidersinteract with each other differently depending on how their Rigidbodycomponents areconfigured. The three important configurations are the StaticCollider (ie,no Rigidbody is attached at all), the RigidbodyCollider andthe KinematicRigidbody Collider.
物件碰撞的互動方式與剛體的設置有關。三種典型的設置是:靜態碰撞物件(不帶剛體),剛體碰撞物件(帶剛體),與自動能剛體碰撞物件(帶剛體Kinematic

StaticCollider

靜態碰撞物""
Thisis a GameObject that has a Collider but no Rigidbody. Staticcolliders are used for level geometry which always stays at the sameplace and never moves around. Incoming rigidbody objects will collidewith the static collider but will not move it.
Thephysics engine assumes that static colliders never move or change andcan make useful optimizations based on this assumption. Consequently,static colliders should not be disabled/enabled, moved or scaledduring gameplay. If you do change a static collider then this willresult in extra internal recomputation by the physics engine whichcauses a major drop in performance. Worse still, the changes cansometimes leave the collider in an undefined state that produceserroneous physics calculations. For example a raycast against analtered Static Collider could fail to detect it, or detect it at arandom position in space. Furthermore, Rigidbodies that are hit by amoving static collider will not necessarily be “awoken” and thestatic collider will not apply any friction. For these reasons, onlycolliders that are Rigidbodies should be altered. If you want acollider object that is not affected by incoming rigidbodies but canstill be moved from a script then you should attacha Kinematic Rigidbodycomponent to it rather than no Rigidbody at all.
物件攜帶碰撞器卻不帶剛體,即是靜態碰撞物件。靜態碰撞物件用在關卡地形中永遠不動的部分。入撞的剛體可撞之,但永遠無法推之。物理引擎會假設靜態碰撞物件永遠不動,這個假設讓物理引擎得以優化。也因此,你不應該在遊戲中使能/失能、移動、縮放靜態碰撞物件。如果你幹了,物理會因重算而導致效能下降。更甚者,畫面上會出現違反物理公式的情況。如果你真的想要用程式移動這類物件,又想要他彈開入撞的剛體,則應該用自動能剛體碰撞物件。

RigidbodyCollider

剛體碰撞物件
Thisis a GameObject with a Collider and a normal, non-kinematic Rigidbodyattached. Rigidbody colliders are fully simulated by the physicsengine and can react to collisions and forces applied from a script.They can collide with other objects (including static colliders) andare the most commonly used Collider configuration in games that usephysics.
物件帶有一般的、非觸發剛體,以及碰撞器時即是。物件完全由物理引擎控制,並可接受程式設置的力學參數。他可以跟其他兩種碰撞物件互動,也是最常見的<帶物理>碰撞物件。


KinematicRigidbody Collider

自動能剛體碰撞物件
Thisis a GameObject with a Collider and a kinematic Rigidbodyattached (ie, the IsKinematic propertyof the Rigidbody is enabled). You can move a kinematic rigidbodyobject from a script by modifying its Transform Component but it willnot respond to collisions and forces like a non-kinematic rigidbody.Kinematic rigidbodies should be used for colliders that can be movedor disabled/enabled occasionally but that should otherwise behavelike static colliders. An example of this is a sliding door thatshould normally act as an immovable physical obstacle but can beopened when necessary. Unlike a static collider, a moving kinematicrigidbody will apply friction to other objects and will “wake up”other rigidbodies when they make contact.
當物件攜帶自動能Kinematic剛體,以及碰撞器時即是。你可以用程式透過調變物件Transform來移動物件,但他本身不會對其他碰撞與力學參數產生反應。這種物件應用在需要移動、使能/失能的物件,不操縱時他就像個靜態碰撞物件。具體舉例,如一扇可開的門,平常是個障礙物,但必要時可用程式開啟。與靜態碰撞物件不同的是,他們具有摩擦力,亦可透過碰撞喚醒休眠中的剛體。
Evenwhen immobile, kinematic rigidbody colliders have different behaviorto static colliders. For example, if the collider is set to as atrigger then you also need to add a rigidbody to it in order toreceive trigger events in your script. If you don’t want thetrigger to fall under gravity or otherwise be affected by physicsthen you can set the IsKinematic propertyon its rigidbody.
這玩意兒在不移動時還是跟靜態碰撞物件有些不同(編按:原文中這一段出現的很奇妙,有如橫空出世。推測想討論的是如何才能使靜態物件之間互撞)。舉例,為了偵測碰撞我們對一個靜態的碰撞物件我們設置了trigger是不夠的,還要設置剛體才能在程式中探知到Trigger。可是加了剛體就會掉下去,所以還要加Kinematic才能夠防止他掉下。
ARigidbody component can be switched between normal and kinematicbehavior at any time using the IsKinematic property.
剛體組件可以透過IsKinematic屬性隨時開啟/關閉kinematic
Acommon example of this is the “ragdoll” effect where a characternormally moves under animation but is thrown physically by anexplosion or a heavy collision. The character’s limbs can each begiven their own Rigidbody component with IsKinematicenabledby default. The limbs will move normallly by animationuntil IsKinematic isswitched off for all of them and they immediately behave as physicsobjects. At this point, a collision or explosion force will send thecharacter flying with its limbs thrown in a convincing way.
一個動態開啟IsKinematic的例子是甩線木偶。平時開啟Kinematic,爆炸或掉落時關閉。

Collisionaction matrix

碰撞互操作列表
Whentwo objects collide, a number of different script events can occurdepending on the configurations of the colliding objects’rigidbodies. The charts below give details of which event functionsare called based on the components that are attached to the objects.Some of the combinations only cause one of the two objects to beaffected by the collision, but the general rule is that physics willnot be applied to an object that doesn’t have a Rigidbody componentattached.


Collision detection occurs and messages are sent upon collision
Static Collider Rigidbody Collider Kinematic Rigidbody Collider Static Trigger Collider Rigidbody Trigger Collider Kinematic Rigidbody Trigger Collider
Static Collider Y
Rigidbody Collider Y Y Y
Kinematic Rigidbody Collider Y
Static Trigger Collider
Rigidbody Trigger Collider
Kinematic Rigidbody Trigger Collider

Trigger messages are sent upon collision
Static Collider Rigidbody Collider Kinematic Rigidbody Collider Static Trigger Collider Rigidbody Trigger Collider Kinematic Rigidbody Trigger Collider
Static Collider Y Y
Rigidbody Collider Y Y Y
Kinematic Rigidbody Collider Y Y Y
Static Trigger Collider Y Y Y Y
Rigidbody Trigger Collider Y Y Y Y Y Y
Kinematic Rigidbody Trigger Collider Y Y Y Y Y Y

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3289572
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

2喜歡★jomo0825 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:Live2D教學#第1節...

追蹤私訊切換新版閱覽

作品資料夾

chanxin95107所有老師們
翻譯菜鳥一枚,放了些蔚藍檔案的翻譯作品在小屋裡,有興趣的歡迎挑看選看( • ̀ω•́ )✧看更多我要大聲說昨天11:18


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】