物理系統概述

Unity2017出來之後,很多有意思的新功能出現了。我想通過這個欄目,整理一下Unity的重點知識。參考Unity的最新文檔。國外的知識都共享,而我看到很多國內教學都是二道販子,用一些沒啥價值的知識虎龍小白。本來就是二手知識(來源於最新官網文檔)還收錢,這也是導致國內外知識傳播時間和空間差距太大的原因之一。樓主水平有限,盡全力去整理總結,本欄目僅僅包含重點知識以及一些有意思的環節。一方面通過博客來提升自己,另一方面,希望能夠幫到一些朋友。如果覺得我的博文有一點點用處,那麼我就認爲我下班後的時間有了意義。與大家共勉,謝謝。

1.物理系統概述
(1)剛體
A.you should apply forces to push the GameObject and let the physics engine calculate the results.
我們應該使用AddForce的方式來觸發引擎的物理計算,來代替腳本當中生硬的transform計算。樓主曾經利用腳本寫過一個ACT小項目,花了很大時間和經歷,有興趣的朋友可以給我投兩個硬幣:
DreamOfWitch項目
完全是腳本寫的,非常費勁,而且不容易出效果。如果能夠充分利用物理引擎會達到一個很好的效果。

B.you may want to control your character directly from script code but still allow it to be detected by triggers (see Triggers below). This kind of non-physical motion produced from a script is known as kinematic motion.
還記得剛體上的Kinematic嗎?這個屬性如果設置爲True,那麼可以讓擁有剛體的物體擺脫物理計算。如果你想用代碼控制物體,但是還想讓物體有剛體,IsKinematic爲True是一個好選擇。

C.When a Rigidbody is moving slower than a defined minimum linear or rotational speed, the physics engine assumes it has come to a halt. When this happens, the GameObject does not move again until it receives a collision or force, and so it is set to “sleeping” mode. This optimisation means that no processor time is spent updating the Rigidbody until the next time it is “awoken” (that is, set in motion again).
需要掌握剛體睡眠的概念。當物體移動的很慢,旋轉的很慢(在一個閾值之內的話),系統默認她爲睡眠狀態。Sleep狀態下,直到再一次受到力或者受到碰撞她纔會WakeUp。
在睡眠狀態下,該物體的剛體不需要處理更新RigidBody的處理器時間。

D.However, a GameObject might fail to wake up if a Static Collider (that is, one without a Rigidbody) is moved into it or away from it by modifying the Transform position. This might result, say, in the Rigidbody GameObject hanging in the air when the floor has been moved out from beneath it. In cases like this, the GameObject can be woken explicitly using the WakeUp function.
如果是一個靜態碰撞體(沒有剛體,只有Collider組件)通過改變Transform的方式移入或者移出物體,不會觸發剛體的物理系統。可以通過腳本WakeUp已經睡着的物體喔。

(2)碰撞體

A. compound colliders can often approximate the shape of an object quite well while keeping a low processor overhead. Further flexibility can be gained by having additional colliders on child objects (eg, boxes can be rotated relative to the local axes of the parent object). When creating a compound collider like this, there should only be one Rigidbody component, placed on the root object in the hierarchy.
複合碰撞體可以使用基礎的碰撞體來模擬複雜的碰撞體。父物體掛在一個剛體,子物體們只需要掛在碰撞體就好了。這種方式還可以降低處理器開銷。
注意這種情況放大、旋轉,可能會破壞碰撞體的結構。

B.a mesh collider will normally be unable to collide with another mesh collider (ie, nothing will happen when they make contact). You can get around this in some cases by marking the mesh collider as Convex in the inspector. This will generate the collider shape as a “convex hull” which is like the original mesh but with any undercuts filled in. The benefit of this is that a convex mesh collider can collide with other mesh colliders so you may be able to use this feature when you have a moving character with a suitable shape.
網格碰撞一般是不會和其他碰撞體產生反應的。如果想讓其產生反映,試試Convex屬性吧,他會產生一個類似於基礎碰撞的閉包。根據情況靈活使用這個特性。

C.Colliders can be added to an object without a Rigidbody component to create floors, walls and other motionless elements of a scene. These are referred to as static colliders. In general, you should not reposition static colliders by changing the Transform position since this will impact heavily on the performance of the physics engine. Colliders on an object that does have a Rigidbody are known as dynamic colliders. Static colliders can interact with dynamic colliders but since they don’t have a Rigidbody, they will not move in response to collisions.
這裏介紹兩個概念:
靜態碰撞:物體只有Collider組件,而沒有RigidBody組件。比如建築,環境可以設置成靜態碰撞。
動態碰撞:物體有Collider和RigidBody組件。
當靜態碰撞和動態碰撞作用的時候,靜態碰撞不會受到碰撞的影響。這也是將其作爲環境碰撞體的原因。

(3)物理材質
A.When colliders interact, their surfaces need to simulate the properties of the material they are supposed to represent. For example, a sheet of ice will be slippery while a rubber ball will offer a lot of friction and be very bouncy. Although the shape of colliders is not deformed during collisions, their friction and bounce can be configured using Physics Materials. Getting the parameters just right can involve a bit of trial and error but an ice material, for example will have zero (or very low) friction and a rubber material with have high friction and near-perfect bounciness.
當物體進行摩擦的時候,可以通過物理材質來模擬冰、橡膠的效果。雖然Collider的形狀不會受影響。

(4)碰撞詳述
A.The physics engine assumes that static colliders never move or change and can make useful optimizations based on this assumption. Consequently, static colliders should not be disabled/enabled, moved or scaled during gameplay. If you do change a static collider then this will result in extra internal recomputation by the physics engine which causes a major drop in performance. Worse still, the changes can sometimes leave the collider in an undefined state that produces erroneous physics calculations. For these reasons, only colliders that are Rigidbodies should be altered. If you want a collider object that is not affected by incoming rigidbodies but can still be moved from a script then you should attach a Kinematic Rigidbody component to it rather than no Rigidbody at all.
關於靜態碰撞體的一些概念:靜態碰撞不應該移動、放縮、enable。這些會造成額外物理引擎資源消耗以及其對碰撞的錯誤判斷。換句話說,只有有動態碰撞體纔可以變化。如果你想讓一個物體不受到其他剛體的影響,但是隻受到腳本的影響,使用運動學碰撞體(Kinematic爲True的那種)。

B.This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders) and are the most commonly used Collider configuration in games that use physics.
關於動態剛體,是引擎當中最常用的碰撞體了。可以受到力,受到其他剛體的影響。

C.This is a GameObject with a Collider and a kinematic Rigidbody attached (ie, the IsKinematic property of the Rigidbody is enabled). You can move a kinematic rigidbody object from a script by modifying its Transform Component but it will not respond to collisions and forces like a non-kinematic rigidbody. Kinematic rigidbodies should be used for colliders that can be moved or disabled/enabled occasionally but that should otherwise behave like static colliders.
關於運動學碰撞器,可以用腳本控制Transform來完成其移動。但是不受到其他非運動學剛體和力的影響。想想滑動門吧…
Even when immobile, kinematic rigidbody colliders have different behavior to static colliders.
運動學剛體可以用來製作固定在空中的碰撞體。類似於馬里奧的天空磚塊。

A Rigidbody component can be switched between normal and kinematic behavior at any time using the IsKinematic property.
同時剛體組件可以在正常的狀態和運動學狀態進行切換,來完成對應的效果。
這裏我舉個栗子。比如一個士兵在巡邏,遊戲人物的身體每一個部分都可以綁定剛體。正常狀態下是在動畫控制下的。此時身體部位是Kinematic爲true的。當這個士兵踩到地雷的時候,所有的剛體都設置爲Kinematic爲false,此時身體的每一部分都是正常物理計算的。在爆炸的效果中,可以模擬令人信服的效果。

(5)關節
You can attach one rigidbody object to another or to a fixed point in space using a Joint component. Generally, you want a joint to allow at least some freedom of motion and so Unity provides different Joint components that enforce different restrictions.

利用關節來允許部分的自由。舉個例子:可以活動的門,鉸鏈等等。

(6)角色控制器
In 3D physics, this type of behaviour can be created using a Character Controller. This component gives the character a simple, capsule-shaped collider that is always upright. The controller has its own special functions to set the object’s speed and direction but unlike true colliders, a rigidbody is not needed and the momentum effects are not realistic.

我並不建議使用Character Controller。雖然他方便,有這個組件就不用RigidBody,但是她不能夠出色的模擬動量效應。如果你精益求精,不建議使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章