字幕列表 影片播放 列印所有字幕 列印翻譯字幕 列印英文字幕 As a general rule, if you're going to have 在正常的情況下遊戲內所有會動的物件 都應該帶是有剛體(Rigidbody)的物件 moving game objects in your game 剛體是讓物件可以被物理所影響的元件 you should make sure that they are 它允許遊戲物件能因重力而掉落 rigidbody objects. 並帶有物理屬性像是 質量(Mass)、阻力(Drag)和速度(Velocity) Rigidbodies are components that allow 當我們替一個遊戲物件加上剛體元件 我們稱此遊戲物件為剛體物件 a game object to be effected by physics. 剛體是觸發所有物理行為的基本元件 They allow the object to fall under gravity, 除此之外該物件也必須有一個碰撞體 才能夠與其它物理物件互動 and have physics properties such as mass, 如果我們的方體物件沒有附有剛體 那它就會一直停留在半空中 drag and velocity. 替它加上剛體後看看會有甚麼改變 When we add a rigidbody component 如同加入其他元件的方式 to a game object we often then refer to it 我們可從檢視下方的Add Component加入 或從主選單上的Component加入 as a rigidbody object. 你可以在物理類別(Physics)找到剛體元件 A rigidbody component is required for any physics 現在我們的球體物件會因重力而掉落 也可以被物理引擎所控制 based interaction, and the game object 或為它加入任何力量(Force) must also have a collider attached 剛體有許多選項設定 in order to interact with other physics objects. 首先有遊戲物件的重量(Mass) 線性阻力(Drag)和旋轉阻力(Angular Drag) Without a rigidbody our power cube will 物件的重量影響物件碰撞的結果 simply hover in mid air. 重量輕的遊戲物件碰到重量重的物件時 會產生較小的物理行為 But let's see what happens when we add one. 線性阻力決定物件在沒有發生 物理行為下停止移動的速度 Like any other component it can be added 可以把它想像成空氣阻力 using the Add Component button at 它是用來計算線性速度的阻力值 at the bottom of the inspector, or from 旋轉阻力影響遊戲物件停止旋轉的速度 可以決定物件旋轉的快與慢 the 'Component' top menu. 舉例來說, 當物件加入扭力使物件旋轉 旋轉阻力就會影響扭力並反應在旋轉速度上 You will find it under the Physics section. 剛體的下一個設定選項是重力 Now our object falls under gravity 只要將重力(Use Gravity)打勾就可以開啟 and can be controlled by the physics engine 重力的設定可以在主選單裡的 Edit -> Project Settings -> Physics找到 and any forces that are applied to it. 重力的設定是一個三維的值 而Y軸預設值是重力的實際值 -9.81 Rigidbodies have numerous options. 因為你可以在此調整全域重力 所以你可以調出不同的環境效果 Firstly there are settings to control the mass, 例如一個低重力的平台 或是將重力放在不同的軸向的解謎遊戲 drag and angular drag of the game object. 舉例來說, 我將重力的Z軸設定為5 The mass of the object effects how collisions 然後你會看到方體物件飛向Z軸的方向 are treated with the object. 運動體(Is Kinematic)決定 物件是否會被物理行為影響 Game objects with a higher mass will react 一般來說, 物理引擎因為效能因素 所以當場景被載入時 less when collided with a lower mass 所有靜態物件(沒有附剛體的物件) 只會被物理引擎檢查一次 game object. 但當你移動一個靜態物件時 The drag of a game object effects how 物理引擎就必須要重新檢查所有的 靜態物件來保持物理的準確性 quickly it will slow down without 這會使效能大幅下降 other interactions. 要避免這樣的問題可以使用運動體 並透過Translate函式讓運動體移動 Think of it like air resistance. 這樣物件就可以在不影響自己的情況下 與其他物件產生物理行為 It's used to determine the rate of a loss 像是打磚塊遊戲裡的移動平台 of linear velocity. 在這個範例裡 我們的方體物件啟用了重力 Similarly, angular drag effects how 當我們開始播放後, 方體物件自然的往下掉落 quickly the game object will slow it's 我們的球體物件 也和方體物件有同樣的設定 angular velocity, i.e. how 如果方體物件關閉了重力就不會往下掉 但它還是會受到其他物件的物理影響 fast it is rotating. 如果我們不想方體物件被其他物件物理所影響 我們可以將Is Kinematic打勾 So for example if you're adding torque 然後透過不斷的修改座標來移動它的位置 to the object to rotate it, 我們建立一個簡單的程式腳本 the angular drag will create resistance 程式腳本內使用了Translate函式 讓方體物件在每一幀都往前移 to this force. The next option is 你可以看到方體物件在有剛體的情況下 可以移動並與其他物件產生物理行為 whether or not the game object is 因為方體物件不斷的向物理引擎回報座標 而不是要物理引擎重新檢視整個場景 effected by gravity. 差值(Interpolate)與推算(Extrapolate)設定 是用來解決物件的不規則位移 By enabling this checkbox we use gravity. 當你使用剛體進行移動時 產生了些許的不規則位移 Settings for gravity can be seen in the 這個時候你可以使用差值設定 利用上一幀的位置來進行平順的位移運算 Edit - Project Settings - Physics area of Unity. 而推算設定是預測 下一幀的座標位置來進行平順的位移 As you can see it's a 3 dimensional vector 接下來的剛體設定是碰撞的偵測類型 which by default has a real world 它包括了非連續性(Discrete) value of -9.81. 連續性(Continuous) 動態連續性(Continuous Dynamic) Because you can customise it globally here 除非你有遇到問題 否則我們建議使用預設的非連續性設定 you could also create interesting effects 連續性設定是讓快速移動的物件 能和靜態物件互動的設定 Such as low gravity for a platformer 而動態連續性設定是讓快速移動的物件 能和其他動態物件互動 or even setting it to a different axis 剛體最後的設定叫約束(Constraint) as part of a puzzle game. 用來約束剛體元件限制 物件對物理所產生的移動和旋轉行為 For example, let's add gravity to the 舉例來說, 俄羅斯方塊裡的方塊 在掉落到底之前必須是可以旋轉的 Z axis by a value of 5. 這個時侯你就可以利用 約束裡的旋轉設定來進行限制 And now the power cube is pulled towards 在這範例裡, 我們的方體物件會往下掉 towards the global Z axis. 因為剛體有啟用了重力 所以它掉落的感覺會像這樣 The Is Kinematic setting effects whether 如果我們不希望他在掉落的過程中旋轉 我們可以把Freeze Rotation都打勾 or not a rigid body will react to physics. 現在它掉落就不會旋轉了 Ordinarily when a scene begins, all static geometry, meaning any non-rigidbody objects are checked once by the physics engine and not checked again for efficiency. However when you move a static object the physics engine must re-check all other static objects for the sake of accuracy, and this can be expensive to performance. To avoid this, Kinematic rigidbody objects can be used and moved via their transform by using the Translate function. This means that you can have physics objects that effect others but are not effected themselves. An obvious example of this would be the paddle in a Pong or Breakout style game. In this example our rigidbody power cube has Use Gravity checked. When we press play, the object falls to the ground. We also have our round prop samoflange ball object, which has a similar component setup. If the power cube does not have gravity then it will not fall under it, but it will be effected by other objects. If we don't want it to be effected by other objects we can use Is Kinematic. And as stated we can also move the object via it's transform. So we'll make use of this simple script, which uses the translate function to move it via it's forward direction every frame. And as you can see, the object still interacts with the others but remains a rigidbody, so is constantly informing the physics engine of it's location and not forcing the physics engine to re-evaluate the entire scene. The Interpolate and Extrapolate settings are there to solve jittering. If you experience slight movement of your object when moving it via it's rigidbody, make use of the interpolate setting in order to smooth the transform movement based on the previous frame. And the extrapolate setting to smooth based on a predicted location in the next frame. The next setting is for the type of collision detection. We have Discrete, Continuous and Continuous Dynamic. The default is discrete and unless you have any problems you should use discrete. Continuous is for fast moving objects that are interacting with static geometry. And continuous dynamic is for fast moving objects that are interacting with other dynamic objects. Finally the constraints section of the rigidbody component allow you to constrain movement or rotation of the object by physics. For example, if you had a Tetris style game you might not want the cubes of your game to rotate as they fell in to place. You could constrain this using the rotation constraints here. In this example our power cube is falling on to the workbench. It's a rigid body that has Use Gravity checked. And as standard it falls like this. If we didn't want it to rotate as it falls we can freeze the rotation within the constraints. And now when it falls, no rotation.
B2 中高級 中文 英國腔 物件 物理 重力 設定 阻力 掉落 物理對象(剛體) - Unity官方教程 (Physics Objects (Rigidbodies) - Unity Official Tutorials) 94 5 朱瑛 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字