2014年10月12日 星期日

[Unity] Rigidbody

官網教學:

=======================================================

Rigidbody

SWITCH TO SCRIPTING
Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make your objects move in a realistic way. Any GameObject must contain a Rigidbody to be influenced by gravity, act under added forces via scripting, or interact with other objects through the NVIDIA PhysX physics engine.


Properties

Property:Function:
MassThe mass of the object (arbitrary units). You should not make masses more or less than 100 times that of other Rigidbodies.
DragHow much air resistance affects the object when moving from forces. 0 means no air resistance, and infinity makes the object stop moving immediately.
Angular DragHow much air resistance affects the object when rotating from torque. 0 means no air resistance. Note that you cannot make the object stop rotating just by setting its Angular Drag to infinity.
Use GravityIf enabled, the object is affected by gravity.
Is KinematicIf enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached.
InterpolateTry one of the options only if you are seeing jerkiness in your Rigidbody’s movement.
        NoneNo Interpolation is applied.
        InterpolateTransform is smoothed based on the Transform of the previous frame.
        ExtrapolateTransform is smoothed based on the estimated Transform of the next frame.
Collision DetectionUsed to prevent fast moving objects from passing through other objects without detecting collisions.
        DiscreteUse Discreet collision detection against all other colliders in the scene. Other colliders will use Discreet collision detection when testing for collision against it. Used for normal collisions (This is the default value).
        ContinuousUse Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection against static MeshColliders (without a rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discreet Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don’t have issues with collisions of fast objects)
        Continuous DynamicUse continuous collision detection against objects set to Continuous and Continuous Dynamic Collision. It will also use continuous collision detection against static MeshColliders (without a rigidbody). For all other colliders it uses discreet collision detection. Used for fast moving objects.
ConstraintsRestrictions on the Rigidbody’s motion:-
        Freeze PositionStops the Rigidbody moving in the world X, Y and Z axes selectively.
        Freeze RotationStops the Rigidbody rotating around the world X, Y and Z axes selectively.

Details

Rigidbodies allow your GameObjects to act under control of the physics engine. This opens the gateway to realistic collisions, varied types of joints, and other very cool behaviors. Manipulating your GameObjects by adding forces to a Rigidbody creates a very different feel and look than adjusting the Transform Componentdirectly. Generally, you shouldn’t manipulate the Rigidbody and the Transform of the same GameObject - only one or the other.
The biggest difference between manipulating the Transform versus the Rigidbody is the use of forces. Rigidbodies can receive forces and torque, but Transforms cannot. Transforms can be translated and rotated, but this is not the same as using physics. You’ll notice the distinct difference when you try it for yourself. Adding forces/torque to the Rigidbody will actually change the object’s position and rotation of the Transform component. This is why you should only be using one or the other. Changing the Transform while using physics could cause problems with collisions and other calculations.
Rigidbodies must be explicitly added to your GameObject before they will be affected by the physics engine. You can add a Rigidbody to your selected object from Components->Physics->Rigidbody in the menubar. Now your object is physics-ready; it will fall under gravity and can receive forces via scripting, but you may need to add a Collider or a Joint to get it to behave exactly how you want.
=================================================================
恩...為了寫文章才去官網找教學發現,果然我也該把官網好好讀一讀才對,我遺漏的關鍵知識實在太多了= =+
好,簡單來說如果希望一個遊戲物件(GameObject)有物理屬性,就要替他增加Rigidbody屬性
(Unity所有物件都是從基本的GameObject,預設只有Transform然後自行附加各種屬性和腳本,製作出符合自己要求的物件)。

有了Rigidbody屬性,物件就會擁有物理特性,像質量、速度、角動量等等
附加Collider之後也能和其他物件產生碰撞,Collider和碰撞會在下一篇文章介紹

這邊要講一個經驗上的重點
當一個有Rigidbody的物體要移動或旋轉
要用Rigidbody類別的AddForce()和AddTorque()去處理
雖然之前提到Transform也可以用其類別中的Translate()和Rotate()去做出移動和旋轉
但是最大的差異是Rigidbody的函數符合物理,是由物理引擎運算出連續的結果
但是Transform類別會製造無視物理的強制移動,以Translate()為例,會直接讓物件強制往前移動指定的單位,並且移動不是連續的,是"直接加上"移動的值,所以物件很可能會直接陷入牆裡,或穿過物體 (簡單來說Transform其實都不是連續移動或旋轉,是類似空間跳躍)

所以如果希望Rigidbody物件以符合物理現象的方式移動和旋轉
一定要用Rigidbody的函數,而不要直接使用Transform的函數

當然反過來向玩家視角的攝影機等
不用附加物理屬性也不用做符合物理的移動就直接使用Transform比較恰當

這個經驗太晚領悟,也太晚看官網教學的代價就是
我目前V2.1.0的遊戲,很多物件和NPC的移動幾乎都要大修了T^T
(之前看他們會陷牆或爬上峭壁還不以為意,以為是真的可以這樣,後來才發現自己錯得很嚴重= =)

沒有留言:

張貼留言