包含5節(jié)視頻教程
本系列包含Unity3D介紹、Unity3D導出人物、Unity3D導出場景、Unity3D導出動作、游戲測試最終完成一個簡單的游戲。是一套非常完整的Unity3D游戲制作教程。
![]()
|
![]() 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. 剛體使物體能在物理控制下運動。剛體可通過接受力與扭矩,使物體像現(xiàn)實方式一樣運動。任何物體想要受重力影響,受腳本施加的力的作用,或通過NVIDIA PhysX物理引擎來與其他物體交互,都必須包含一個剛體組件。
Properties 屬性
Details 細節(jié)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 Component directly. Generally, you shouldn't manipulate the Rigidbody and the Transform of the same GameObject - only one or the other. 剛體讓物體在物理引擎控制下運動。它可以通過真實碰撞來開門,實現(xiàn)各種類型的關節(jié)及其他很酷的行為。通過力來操縱物體,與直接通過變換不同,有一種不同的感覺。通常情況下,對同一物體,要么通過剛體操縱,要么通過變換操縱。 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. 通過變換與通過剛體操縱最大的不同在于使用了力。剛體相比于變換,能夠接受力與扭力。變換能夠控制平移和旋轉,但與物理方式的實現(xiàn)不同。你可以通過實踐來明顯地區(qū)分它們的不同。給剛體施加力和力矩實際也會改變物體變換組件的位置與旋轉角度。這就是為什么在變換和剛體間,最好只選擇一種操縱方式的原因。同時使用兩種方式會導致旋轉及其他計算出現(xiàn)問題。 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. 剛體在受物理引擎影響之前,必須明確添加給物體。你可以通過選中物體,然后在菜單Components->Physics->Rigidbody來增加一個剛體組件�,F(xiàn)在,你的物體的物理屬性就設置好了,但你也可以根據(jù)需要來為其增加碰撞器或關節(jié)。 Parenting 父子級When an object is under physics control, it moves semi-independently of the way its transform parents move. If you move any parents, they will pull the Rigidbody child along with them. However, the Rigidbodies will still fall down due to gravity and react to collision events. 當一個物體處于物理控制中,他會以半獨立的方式隨著變換的父親的移動而移動。如果你移動父物體,那么它會將子剛體拖向它。但是,這個剛體仍然會根據(jù)重力下落或進行碰撞反應。 Scripting 腳本To control your Rigidbodies, you will primarily use scripts to add forces or torque. You do this by calling AddForce() and AddTorque() on the object's Rigidbody. Remember that you shouldn't be directly altering the object's Transform when you are using physics. 可以通過腳本增加力或力矩來控制你的剛體。通過調(diào)用剛體中的AddForce()和AddTorque方法。 記住,不要同時使用物理與變換。 Animation 動畫For some situations, mainly creating ragdoll effects, it is neccessary to switch control of the object between animations and physics. For this purpose Rigidbodies can be marked isKinematic. While the Rigidbody is marked isKinematic, it will not be affected by collisions, forces, or any other part of physX. This means that you will have to control the object by manipulating the Transform component directly. Kinematic Rigidbodies will affect other objects, but they themselves will not be affected by physics. For example, Joints which are attached to Kinematic objects will constrain any other Rigidbodies attached to them and Kinematic Rigidbodies will affect other Rigidbodies through collisions. 有些情況下,比如想實現(xiàn)布娃娃效果。需要剛體在動畫與物理的操縱方式間切換。這種情況下,剛體可以標記為運動學模式。當剛體標記為運動學模式,他不會受到碰撞,力及任何物理影響,它表示你必須直接通過變換的方式來操縱該物體。運動學模式的剛體會與其他物體進行物理交互,但自身不受物理影響。例如,通過關節(jié)約束那些和運動學剛體連接起來的剛體,運動學剛體影響那些與之發(fā)生碰撞的剛體。 Colliders 碰撞器Colliders are another kind of component that must be added alongside the Rigidbody in order to allow collisions to occur. If two Rigidbodies bump into each other, the physics engine will not calculate a collision unless both objects also have a Collider attached. Collider-less Rigidbodies will simply pass through each other during physics simulation. 碰撞器是另一種組件,它和剛體一起,來使碰撞發(fā)生。如果兩個剛體撞在一起,物理引擎將不會計算碰撞除非它們包含一個碰撞器組件。沒有碰撞器的剛體,會在物理模擬中相互穿透。
Add a Collider with the Component->Physics menu. View the Component Reference page of any individual Collider for more specific information: 通過菜單Component->Physics menu來添加一個碰撞器。想知道更多信息,請看參考手冊中碰撞器方面的介紹。
Compound Colliders 復合碰撞器Compound Colliders are combinations of primitive Colliders, collectively acting as a single Collider. They come in handy when you have a complex mesh to use in collisions but cannot use a Mesh Collider. To create a Compound Collider, create child objects of your colliding object, then add a primitive Collider to each child object. This allows you to position, rotate, and scale each Collider easily and independently of one another. 組合碰撞器是基本碰撞器的組合,共同扮演一個碰撞器的角色。當你有一個復雜網(wǎng)格卻不適用網(wǎng)格碰撞器的情況下,使用組合碰撞器是個好的選擇。要創(chuàng)建組合碰撞器,先為你的碰撞物體創(chuàng)建子對象,然后對每個子對象創(chuàng)建一個基本碰撞器。這樣就允許你輕易的獨立移動、旋轉和放縮每個碰撞器。
In the above picture, the environment has a Mesh Collider attached. Mesh Colliders work the best for terrain or environments made from irregular shapes. The gun_model GameObject has a Rigidbody attached, and multiple primitive Colliders as child GameObjects. When the Rigidbody parent is moved around by forces, the child Colliders move along with it. The primitive Colliders will collide with the environment's Mesh Collider, and the parent Rigidbody will alter the way it moves based on forces being applied to it and how its child Colliders interact with other Colliders in the Scene. 上圖中,環(huán)境里包含一個網(wǎng)格碰撞器。網(wǎng)格碰撞器的最佳適用地方是不規(guī)則形狀的地形或環(huán)境。如圖,槍模型物體包含一個剛體組件,以及數(shù)個基本碰撞器的子物體。當剛體這個父對象受力移動時,它的子碰撞器也跟著移動�;九鲎财髋c環(huán)境的網(wǎng)格碰撞器發(fā)生碰撞時,父對象的剛體也會因其子碰撞器與環(huán)境中其他碰撞器發(fā)生碰撞產(chǎn)生的力而改變運動軌跡 。 Mesh Colliders can't normally collide with each other. If a Mesh Collider is marked as Convex, then it can collide with another Mesh Collider. The typical solution is to use primitive Colliders for any objects that move, and Mesh Colliders for static background objects. 網(wǎng)格碰撞器之間通常不相互碰撞,但如果一個網(wǎng)格碰撞器被標記為凸體,那么它就可以與其他網(wǎng)格碰撞器碰撞。典型的解決方案是,對移動的對象使用基本碰撞器,而對靜態(tài)環(huán)境對象使用網(wǎng)格碰撞器。 Continuous Collision Detection 連續(xù)碰撞檢測Continuous collision detection is a feature to prevent fast-moving colliders from passing each other. This may happen when using normal (Discrete) collision detection, when an object is one side of a collider in one frame, and already passed the collider in the next frame. To solve this, you can enable continuous collision detection on the rigidbody of the fast-moving object. Set the collision detection mode to Continuous to prevent the rigidbody from passing through any static (ie, non-rigidbody) MeshColliders. Set it to Continuous Dynamic to also prevent the rigidbody from passing through any other supported rigidbodies with collision detection mode set to Continuous or Continuous Dynamic. Continuous collision detection is supported for Box-, Sphere- and CapsuleColliders. Note that continuous collision detection is intended as a safety net to catch collisions in cases where objects would otherwise pass through each other, but will not deliver physically accurate collision results, so you might still consider decreasing the fixed Time step value in the TimeManager inspector to make the simulation more precise, if you run into problems with fast moving objects. 連續(xù)碰撞檢測的主要作用是避免高速物體的穿透。在不連續(xù)碰撞檢測模式下,一個高速物體的上一幀位置在一個碰撞器的一邊,而下一幀位置就穿透該碰撞器時,就發(fā)生了穿透。為了解決這個問題,你要為高速物體設置連續(xù)碰撞檢測模式。設置連續(xù)碰撞檢測模式將避免剛體穿透靜態(tài)網(wǎng)格碰撞器(該網(wǎng)格未必是剛體)。而設置為連續(xù)動態(tài)碰撞檢測將避免剛體穿透設為連續(xù)動態(tài)碰撞檢測與動態(tài)碰撞檢測的剛體。盒碰撞器、球碰撞器技膠囊碰撞器支持動態(tài)碰撞檢測。主要,連續(xù)碰撞檢測只是提供了一種穿透的保護,它保證捕獲碰撞事件,但并不保證碰撞反應的精確性。所以,當你發(fā)現(xiàn)高速物體有這方面問題時,得考慮降低TimeManager中的固定時間間隔來讓模擬更加的精確。 Use the right size 使用正確的大小The size of the your GameObject's mesh is much more important than the mass of the Rigidbody. If you find that your Rigidbody is not behaving exactly how you expect - it moves slowly, floats, or doesn't collide correctly - consider adjusting the scale of your mesh asset. Unity's default unit scale is 1 unit = 1 meter, so the scale of your imported mesh is maintained, and applied to physics calculations. For example, a crumbling skyscraper is going to fall apart very differently than a tower made of toy blocks, so objects of different sizes should be modeled to accurate scale. 你的物體網(wǎng)格的大小要比剛體的質(zhì)量更加重要。如果你發(fā)現(xiàn)你的剛體不像你所期望的那樣運動——慢、飄、碰撞不正確——那么請考慮調(diào)整你的網(wǎng)格資源的大小。Unity的默認長度單位是1單位=1米。你導入模型的大小會被保持,并參與物理運算中。例如,一個摩天大樓倒塌的表現(xiàn)肯定要異于一個玩具積木搭起來的塔,所以,對不同大小物體,要用精確的比例建模。 If you are modeling a human make sure he is around 2 meters tall in Unity. To check if your object has the right size compare it to the default cube. You can create a cube using GameObject->Create Other->Cube. The cube's height will be exactly 1 meter, so your human should be twice as tall. 如果對一個人體建模,那么保證它大概在Unity中2米高�?梢院鸵粋€默認的盒子對比來判斷大小。使用 GameObject->Create Other->Cube來創(chuàng)建盒子。盒子的高為1米,所以你的人高度應該是它的兩倍。 If you aren't able to adjust the mesh itself, you can change the uniform scale of a particular mesh asset by selecting it in Project View and choosing Assets->Import Settings... from the menubar. Here, you can change the scale and re-import your mesh. 如果你不能自己調(diào)整網(wǎng)格,那你可以修改一個指定網(wǎng)格資源的比例,通過在項目視圖中選中它,然后在目錄里選擇Assets->Import Settings,在這里,你可以選擇比例,然后重新加載你的網(wǎng)格。 If your game requires that your GameObject needs to be instantiated at different scales, it is okay to adjust the values of your Transform's scale axes. The downside is that the physics simulation must do more work at the time the object is instantiated, and could cause a performance drop in your game. This isn't a terrible loss, but it is not as efficient as finalizing your scale with the other two options. Also keep in mind that non-uniform scales can create undesirable behaviors when Parenting is used. For these reasons it is always optimal to create your object at the correct scale in your modeling application. 如果物體在游戲中需要實例化成不同的比例,可以調(diào)整變換組件的比例軸。這種做法的缺點是在物體實例化時,物理模擬將執(zhí)行更多操作,會導致游戲性能下降。 這不是很可怕的下降,但確實沒有采用其他兩種方法那樣有效率。此外,父對象使用一個非標準比例的物體也會產(chǎn)生不好的行為。所以,建議是創(chuàng)建物體時盡量調(diào)整好標準比例。 Hints 提示
贊0 踩0 |
未知用戶
2005-2025 朱峰社區(qū) 版權所有 遼ICP備2021001865號-1
2005-2025 ZhuFeng Community All Rights Reserved
VIP