狠狠撸

狠狠撸Share a Scribd company logo
2D 物理關節應用
Revised on December 25, 2019
? Distance Joint
? Fixed Joint
? Friction Joint
? Hinge Joint
? Relative Joint
? 狠狠撸r Joint
? Spring Joint
? Target Joint
? Wheel Joint
? 綜合應用
? https://docs.unity3d.com/Manual/Joints2D.html
? 物理關節用來將遊戲物件與另?遊戲物件或場景空間的固定位置建
立物理連結
2D Joints 1/3
2
? 適合用來建構場景上的機關陷阱
? 套用Joint 2D之物件會自動加上
Rigidbody 2D性質
? 選單命令Component> Physical 2D子選單,可加入2D Joints元件
? 距離關節 (Distance Joint 2D)
將兩個物體以固定相對距離方式連接
? 固定關節 (Fixed Joint 2D)
使兩個物件維持固定的相對位置及角度,很類似階層裡的父子結構
? 摩擦關節 (Friction Joint 2D)
在相連的兩個遊戲物件間加上摩擦力,使移動物件的直線速度及角速度減
慢至停止
? 樞紐關節 (Hinge Joint 2D)
將剛體物件附接到它可以旋轉的?點。例如:?把剪?的樞軸,?旋轉軸
2D Joints 2/3
3
? 相對關節 (Relative Joint 2D)
將兩個剛體遊戲物件彼此保持在相同的相對位置。使用此關節可保持兩個
物體相互偏移。例如:在射擊遊戲中,玩家有額外的槍電池跟隨他們
? 滑塊關節 (狠狠撸r Joint 2D)
限制遊戲物件沿著空間中的直線路徑滑動,例如滑動?
? 彈簧關節 (Spring Joint 2D)
將兩個剛體遊戲物件好像通過彈簧連接在?起
? 目標關節 (Target Joint 2D)
類似相對關節,但連結具有彈性,用來利用回彈力將物體回到目標位置。
可用於拾取和移動例如在重力作用下的物體
? 車輪接頭 (Wheel Joint 2D)
模擬車輪和懸架
2D Joints 3/3
4
? 距離關節可讓遊戲物件與定點或另?物件之間維持固定距離
Distance Joint 2D 1/4
5
Distance Joint 2D
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
Distance Joint所要連接的另?物件,None表示要連接到世界座標
的定點(預設為原點)
? Auto Configure Connected Anchor
勾選時,會自動調整關節所連接物件之?點(Connected Anchor值)
? Anchor
關節?點座標(以圖片pivot為原點),預設值為物件中心點,也允許
於設在物件外部
? Connected Anchor
關節在另?個物件的?點
Distance Joint 2D 2/4
6
Anchor
Connected Anchor
Distance Joint 2D
? Auto Configure Distance
自動調整關節距離(Distance值),預設為勾選
? Distance
Anchor到Connected Anchor之距離
? Max Distance Only
只限制最大距離,連接的物件仍可彼此靠近,但不能超過Distance欄
設定值。若未勾選,則連接的兩物件永遠保持固定距離
Distance Joint 2D 3/4
7
? Break Force
? 斷開關節所需的最小力量,預設值為infinite
? 當關節的reactionForce值(只能在程式腳本讀取)高於breakForce,或
reactionTorque值(只能在程式腳本讀取)高於關節的breakTorque時,
關節將斷裂
? 當關節斷裂時,系統會執行OnJointBreak2D事件程序,並且傳入
Joint2D。完成OnJointBreak2D事件程序後,關節將自動從遊戲物件中
移除並刪除
Distance Joint 2D 4/4
8
? 將Sprites/ball加入到場景
? Position(X, Y, Z) = (-3, -3, 0)
? Scale(X, Y) = (0.5, 0.5)
? 在ball物件加入Distance Joint 2D元件
? 選單命令Component> Physics 2D> Distance Joint 2D
? 取消勾選Auto Configure Distance
? 執行測試,ball會以場景中心為?點擺盪
Distance Joint 2D練習 1/3
9
rope end
Anchor of
Distance Joint 2D
Distance Joint 2D
Connected Anchor of
Distance Joint 2D
? 將Sprite/rope end加入到場景
? Position(X, Y, Z) = (-1, 0, 0)
? 在rope end物件加入運動剛體元件
? 選單命令Component> Physics 2D> Rigidbody 2D
? Body Type = Kinematic
? 將rope end物件拖曳到ball之Distance Joint 2D的Connected
Rigid Body屬性欄
? 執行測試,ball會以rope end為?點擺盪
Distance Joint 2D練習 2/3
10
rope end
Distance Joint 2D
ball
Connected Rigid Body
? 將ball移到rope end上方
? Position(X, Y, Z) = (3, -3, 0)
? 勾選ball之Distance Joint 2D的Max Distance Only
? 執行測試,觀察ball運動情形
? 反覆調整ball之Distance Joint 2D的Break Force值,執行測試,
觀察ball運動情形
Distance Joint 2D練習 3/3
11
rope end
Distance Joint 2D
ball
Connected Rigid Body
? 固定關節用來將2個物件以軸承方式連接在?起
Fixed Joint 2D 1/3
12
Fixed Joint 2D
Fixed Joint 2D
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?物件,None表示要連接到世界座標的定點
? Auto Configure Connected Anchor
自動調整關節所連接物件之?點(Connected Anchor值)
? Anchor
關節?點,預設值為物件中心點,也允許於設在物件外部
? Connected Anchor
關節在另?個物件的?點
Fixed Joint 2D 2/3
13
? Damping Ratio
阻尼比,0.0~1.0之間。阻尼比愈大,物件受力振動後愈快回復原狀
? Frequency
物件振動頻率,0~1,000,000之間,數值愈大,物件愈不易來回振動
(0表示完全不振動)
? Break Force
斷開關節所需的最小力道
? Break Torque
斷開關節所需的最小扭力
Fixed Joint 2D 3/3
14
? 將Sprites/ball加到場景
? Scale(X, Y, Z) = (0.5, 0.5, 1)
? Position(X, Y, Z) = (-1.5, 3, 0)
? 在ball物件加入Rigidbody 2D元件
? 選單命令Component> Physics 2D> Rigidbody 2D
? 在ball物件加入Circle Collider 2D元件
? 選單命令Component> Physics 2D> Circle Collider 2D
? 將Sprites/plank加到場景,命名為plank a
? Position(X, Y, Z) = (0, 0, 0)
? 將Sprites/plank加到場景,命名為plank b
? Position(X, Y, Z) = (-2, -2, 0)
Fixed Joint 2D練習 1/2
15
? 在plank a物件加入Box Collider 2D元件
? 在plank a物件加入Fixed Joint 2D元件
? Anchor(X, Y) = (1.35, 0)
? Connected Anchor(X, Y) = (1.35, 0)
? Frequency = 5
? 在plank b物件加入Box Collider 2D元件
? 在plank b物件加入Fixed Joint 2D元件
? Anchor(X, Y) = (0, 0)
? Connected Anchor(X, Y) = (-2, -2)
? Frequency = 0.1
? 執行測試,觀察ball掉落運動情形;調整Frequency及Damping
Ratio,觀察plank振動情形
Fixed Joint 2D練習 2/2
16
ball
ball
Anchor of Fixed Joint 2D
plank a
plank b
Anchor of Fixed Joint 2D
? 摩擦關節來在運動物件加上線摩擦力和角摩擦力
Friction Joint 2D 1/3
17
Distance Joint 2D
Friction Joint 2D
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?物件,None表示要連接到世界座標的定點
? Auto Configure Connected Anchor
自動調整關節所連接物件之?點(Connected Anchor值)
? Anchor
關節?點,預設為物件中心點,也允許於設在物件外部
? Connected Anchor
關節在另?個物件的?點
Friction Joint 2D 2/3
18
? Max Force
設定直線移動摩擦力,預設值為1
? Max Torque
設置旋轉摩擦力,預設值為1
? Break Force
斷開關節所需的最小力道
? Break Torque
斷開關節所需的最小扭力
Friction Joint 2D 3/3
19
upper plank
? 將Sprites/ball加入到場景
? Position(X, Y, Z) = (-3, -1, 0)
? Scale(X, Y) = (0.5, 0.5)
? 在ball物件加入Distance Joint 2D元件
? 選單命令Component> Physics 2D> Distance Joint 2D
? 執行測試,ball會以場景中心為?點擺盪
? 在ball物件加入Friction Joint 2D
? 取消勾選Auto Configure Connected Anchor
? Max Force = 0.5,Max Torque = 0.5
? Connected Anchor(X, Y) = (0, -4)
? 執行測試,ball是否較快停止擺盪
Friction Joint 2D練習
20
Distance Joint 2D
Friction Joint 2D
Connected Anchor
Connected Anchor
? 樞紐關節可讓物件繞著轉軸旋轉
Hinge Joint 2D 1/3
21
Distance Joint 2DHinge Joint 2D
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?剛體物件,None表示要連接到世界座標的定點
? Auto Configure Connected Anchor
自動調整關節所連接到的物件之?點(Connected Anchor值)
? Anchor
剛體物體的旋轉點
? Connected Anchor
剛體物體的旋轉點所附著的位置
Hinge Joint 2D 2/3
22
? Use Motor 勾選表示在樞紐關節加上馬達動力
? Motor Speed
馬達速度,角度值/每秒,負值為逆時針
? Maximum Motor Force
馬達最大扭力
? Use Limits 勾選表示在樞紐關節加上旋轉角度限制
? Lower Angle
樞紐旋轉角度下限(以Anchor為圓心)
? Upper Angle
樞紐旋轉角度上限(以Anchor為圓心)
Hinge Joint 2D 3/3
23
Anchor
Upper Angle
Lower Angle
? 將Sprites/ball加到場景
? Scale(X, Y, Z) = (0.5, 0.5, 1)
? Position(X, Y, Z) = (-2, 3, 0)
? 加入Circle Collider 2D元件
? 選單命令Component> Physics 2D> Circle Collider 2D
? 加入Distance Joint 2D元件
? 選單命令Component> Physics 2D> Distance Joint 2D
? 將Sprites/plank加到場景
? Position(X, Y, Z) = (-5, 0, 0)
? 加入Box Collider 2D
Hinge Joint 2D練習 1/2
24
? 在plank加上Hinge Joint 2D元件
? Anchor(X, Y) = (-1, 0)
? Connected Anchor = (-6, 0)
? 勾選Use Motor
? Motor Speed:-30
? Maximum Motor Force:15
? 勾選Use Limits
? Lower Angle:0
? Upper Angle:45
? 執行測試,觀察plank活動情形
Hinge Joint 2D練習 2/2
25
Upper Angle
Lower Angle
Distance Joint 2D
Anchor of Hinge
Jointer 2D
? 使用Hinge Joint 2D建立鏈條
Hinge Joint 2D練習II 1/2
26
Hinge Joint 2D練習II 2/2
27
? 使兩個物件間保持在?定的相對位置。例如,寵物自動跟隨玩家
Relative Joint 2D 1/3
28
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?剛體物件,None表示要連接到世界座標的定點
? Max Force
關節連接力道,愈大值表示兩物件愈牢牢連接
? Max Torque
關節連接扭力,愈大值表示兩物件愈牢牢連接
Relative Joint 2D 2/3
29
? Correction Scale
修正相對位置所施加的力道大小,0.0~1.0之間,預設值為0.3。力道
愈大,修正相對位置時擺盪情形愈明顯
? Auto Configure Offset
自動調整關節所連接到的物件之距離偏移(Linear Offset)及角度偏
移(Angular Offset)
? Linear Offset
與關節所連接物件之距離偏移值
? Angular Offset
與關節所連接物件之角度偏移值
Relative Joint 2D 3/3
30
? 將Sprites/plank加到場景
? Position (X, Y, Z)=(0, -3.5, 0)
? Rotation (X, Y, Z)=(0, 0, -4)
? Scale (X, Y, Z)=(4, 1, 1)
? 加入Box Collider 2D
? 選單命令Component> Physics 2D> Box Collider 2D
? 將Sprites/ball加入到場景
? Position(X, Y, Z) = (-4, 1, 0)
? Scale(X, Y, Z) = (0.3, 0.3, 1)
? 加入Rigidbody 2D
? 選單命令Component> Physics 2D> Rigidbody 2D
Relative Joint 2D練習 1/2
31
? 將Sprites/ball加到場景,命名為ring
? Position (X, Y, Z) = (-4, 2, 0)
? Scale (X, Y, Z) = (0.15, 0.15, 1)
? 加入Circle Collider 2D元件
? 加入Realtive Joint 2D元件
? Connected Rigid Body = ball
? Max Force=100
? Correction Scale = 0.1
? 勾選Auto Configure Offset
? 複製7個ring物件,環繞ball成圓形
? 執行測試,觀察ball及ring活動情形
Relative Joint 2D練習 2/2
32
? 可讓物體回應於碰撞或施力而滑動,例如滑動?
狠狠撸r Joint 2D 1/5
33
Distance Joint 2D
狠狠撸r Joint 2D
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?剛體物件,None表示要連接到世界座標的定點
? Auto Configure Connected Anchor
自動調整關節所連接到的物件之?點(Connected Anchor值)
? Anchor
關節?點,預設值為物件中心點,也允許於設在物件外部
? Connected Anchor
關節連接到場景上的?點
狠狠撸r Joint 2D 2/5
34
? Auto Configure Angle
自動調整與關節所連接物件之角度值(Angle)
? Angle
與關節所連接物件之角度值
? Use Motor 勾選表示在關節施加馬達動力
? Motor Speed
馬達速度 (公尺/秒),正值朝Lower Translation方向移動,負值朝
Upper Translation方向移動
? Maximum Motor Force
馬達最大力道
狠狠撸r Joint 2D 3/5
35
? Use Limits 勾選表示啟用關節滑動距離限制
? Lower Translation
滑動時Connected Anchor的下邊界位置(以Anchor值為原點)
? Upper Translation
滑動時Connected Anchor的上邊界位置(以Anchor值為原點)
? Break Force
斷裂力道,破壞關節所需的最小力量
? Break Torque
斷裂扭力,破壞關節所需的最小扭力
狠狠撸r Joint 2D 4/5
36
狠狠撸r Joint 2D 5/5
37
當Motor speed為正值時
當Motor speed為負值時
Lower
Translation
Upper
Translation
Connected Anchor
Connected Anchor
Connected Anchor
? 將Sprites/ball加到場景
? Scale(X, Y, Z) = (0.3, 0.3, 1)
? Position(X, Y, Z) = (-1.414, 1.414, 0)
? 加入Circle Collider 2D元件
? 選單命令Component> Physics 2D> Circle Collider 2D
? 加入Distance Joint 2D元件
? 選單命令Component> Physics 2D> Distance Joint 2D
? Break Force = 50
? 將Sprites/plank加到場景
? Position(X, Y, Z) = (1.5, -2, 0)
? 加入Box Collider 2D
狠狠撸r Joint 2D練習 1/2
38
? 在plank物件加入狠狠撸r Joint 2D
? 取消勾選Auto Configure Connected Anchor
? Anchor(X, Y) = (0, 0)
? Connected Anchor(X, Y) = (0, -2)
? Angle = 180
? 勾選Use Motor
? Motor Speed:-1,Maximum Motor Force:2
? 勾選Use Limits
? Lower Translation:1.5
? Upper Translation:4.5
? 執行測試,觀察plank活動情形
狠狠撸r Joint 2D練習 2/2
39
Upper
Translation
Connected Anchor
AnchorLower
Translatio
n
Distance Joint 2D
? 彈簧關節將物件如同透過彈簧連接在?起
Spring Joint 2D 1/3
40
Spring Joint 2D
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?剛體物件,None表示要連接到世界座標的定點
? Auto Configure Connected Anchor
自動調整關節所連接到的物件之?點(Connected Anchor值)
? Anchor
關節?點,預設值為物件中心點,也允許於設在物件外部
? Connected Anchor
關節在另?個物件的?點
Spring Joint 2D 2/3
41
? Auto Configure Distance
自動調整Distance值。要有彈簧效果,不可勾選此項
? Distance
彈簧?度
? Damping Ratio
阻尼比,0.0~1.0之間。阻尼比愈大,彈簧愈快回復靜止狀態
? Frequency
彈簧振動的頻率,0~1,000,000之間,值愈大,表示彈簧愈硬
Spring Joint 2D 3/3
42
? 將Sprites/plank加到場景
? Position(X, Y, Z) = (0, 3, 0)
? 加入Rigidbody 2D
? Body Type = Kinematic
? 將Sprites/ball加到場景
? Position(X, Y, Z) = (0, -2, 0)
? Scale(X, Y, Z) = (0.4, 04, 0)
? 加入Spring Joint 2D
? Connected Rigid Body = plank
? 取消勾選Auto Configure Distance
? 執行測試,使用滑鼠拖曳ball再放開,觀察ball擺盪情形
Spring Joint 2D練習
43
Connected Rigid Body
Spring Joint 2D
Distance
Anchor
? 是?種近似彈簧關節,利用回彈力讓物體返回定點
Target Joint 2D 1/3
44
? Anchor
關節?點,預設值為物件中心點,也允許於設在物件外部
? Target
物體的目標點
? Auto Configure Target
自動調整Target值
? Max Force
朝向目標點移動的力道
? Damping Ratio
阻尼比,0.0~1.0之間,阻尼比愈大表示來回運動次數愈少
Target Joint 2D 2/3
45
? Frequency
振動的頻率,0~1,000,000之間,值愈大,表示彈簧愈硬
? Break Force
斷裂力道,破壞關節所需的最小力量
Target Joint 2D 3/3
46
? 將Sprites/ball加到場景
? Position (X, Y, Z) = (-7.5, 4, 0)
? Scale (X, Y, Z) = (0.4, 0.4, 1)
? 加入Rigidbody 2D
? 加入Circle Collider 2D
? 將Sprites/plank加到場景
? Position (X, Y, Z) = (-8, 2, 0)
? Rotation (X, Y, Z) = (0, 0, -1)
? Scale (X, Y, Z) = (0.6, 1, 1)
? 加入Rigidbody 2D
? 勾選Freeze Rotation Z
? 加入Box Collider 2D
Target Joint 2D練習 1/2
47
? 加入Target Joint 2D
? 勾選Auto Configure Target
? Max Force = 30
? Damping Ratio = 0.1
? Frequency = 3
? 複製6個plank物件,排列成階梯狀;執行測試,觀察執行結果
Target Joint 2D練習 2/2
48
? 車輪關節用來模擬滾動輪,車輪關節可加上動力,使車輪關節上的物
體移動。車輪關節使用懸吊"彈簧"來保持其與車輛主體的距離
Wheel Joint 2D 1/3
49
? Enable Collision
啟用關節所連接的兩個物件間的碰撞偵測
? Connected Rigid Body
關節所要連接的另?剛體物件,None表示要連接到世界座標的定點
? Auto Configure Connected Anchor
自動調整關節所連接到的物件之?點(Connected Anchor值)
? Anchor
關節?點,預設值為物件中心點,也允許於設在物件外部
? Connected Anchor
關節在另?個物件的?點
Wheel Joint 2D 2/3
50
? Suspension 車輪懸吊系統設定
? Damping Ratio
阻尼比,0.0~1.0之間,阻尼比愈大表示彈簧來回運動次數愈少
? Frequency
彈簧振動的頻率,0~1,000,000之間,值愈大,表示彈簧愈硬
? Angle
彈簧懸吊角度
? Use Motor 施加馬達動力
? Motor Speed
馬達速度 (角度值/秒)
? Maximum Motor Force
馬達最大力道
Wheel Joint 2D 3/3
51
? 將Sprites/plank加到場景
? Position(X, Y, Z) = (-7.5, 0, 0)
? 加入Rigidbody 2D
? 將Sprites/car wheel加到場景
? Position(X, Y, Z) = (-8.5, -1.2, 0)
? Scale(X, Y, Z) = (0.5, 0.5, 1)
? 加入Wheel Joint 2D
? Connected Rigid Body = plank
? 取消勾選Auto Configure Connected Anchor
? Anchor(X, Y) = (0, 0)
? Connected Anchor(X, Y) = (-1, -1.25)
Wheel Joint 2D練習 1/2
52
? 勾選Use Motor
? Motor Speed = 10
? 加入Circle Collider 2D
? 複製car wheel
? Position(X, Y, Z) = (-6.5, -1.25, 0)
? Wheel Joint 2D
? Connected Anchor(X, Y) = (1, -1.25)
? 將Sprites/plank加到場景
? Position(X, Y, Z) = (0, -3, 0)
? Scale(X, Y, Z) = (7, 1, 1)
? 加入Box Collider 2D
Wheel Joint 2D練習 2/2
53
2D Joint應用練習
54
? 設定Main Camera
? Size = 10
? 將background加入場景
? Position(X, Y, Z) = (0, 0, 0)
? Scale(X, Y, Z) = (5, 5, 1)
? Order in Layer = -1
建置場景 1/2
55
? 在background加入Edge Collider 2D
? Edge Radius = 0.1
? 編輯Edge Collier線段,使其符合背景地形
建置場景 2/2
56
Edge Collider 2D
? 將car body加到場景,命名為truck
? 重置Transform
? Order in Layer = 1
? tag = Player
? 將suspension加到場景,命名為front suspension
? Position(X, Y, Z) = (3.77, -0.3, 0)
? Scale(X, Y, Z) = (1.5, 1, 1)
? Order in Layer = 2
? 將suspension加到場景,命名為rear suspension
? Position(X, Y, Z) = (-3.25, -0.3, 0)
? Scale(X, Y, Z) = (1.5, 1, 1)
? Order in Layer = 2
組合卡車 1/3
57
? 將car wheel加到場景,命名為front wheel
? Position(X, Y, Z) = (3.82, -1.85, 0)
? Scale(X, Y, Z) = (1.3, 1.3, 1)
? Order in Layer = 3
? 將car wheel加到場景,命名為rear wheel
? Position(X, Y, Z) = (-3.28, -1.9, 0)
? Scale(X, Y, Z) = (1.3, 1.3, 1)
? Order in Layer = 3
組合卡車 2/3
58
? 將front suspension、rear suspension、front wheel及rear
wheel拖曳成為turck子物件
? 調整turck尺寸
? Scale(X, Y, Z) = (0.7, 0.7, 1)
組合卡車 3/3
59
? truck加入Rigidbody 2D
? 取消勾選Use Auto Mass
? Mass = 5
? truck加入Polygon Collider 2D
? 編輯Polygon Collider,僅量用較少的端點
來覆蓋車體外形
? 保留點數最多的Element
? ctrl-click刪除端點
設置卡車碰撞器 1/2
60
Circle Collider 2D
Edge Collider 2D
? 在front wheel及rear wheel加入Circle Collider 2D
設置卡車碰撞器 2/2
61
? front wheel加入Wheel Joint 2D
? Connected Rigid Body = truck
? 取消勾選Auto Configure Connected Anchor
? Connected Anchor(X, Y) = (3.82, -1.85)
? Suspension/Damping Ratio = 0.7、Frequency = 10、Angle = 90
? 勾選Use Motor
? Moter/Motor Speed = 600、Maximum Motor Force = 1000
? Ririgbody 2D
? 勾選Use Auto Mass
設置Wheel Joint 2D 1/3
62
? rear wheel加入Wheel Joint 2D
? Connected Rigid Body = truck
? 取消勾選Auto Configure Connected Anchor
? Connected Anchor(X, Y) = (-3.28, -1.9)
? Suspension/Damping Ratio = 0.7、Frequency = 10、Angle = 90
? 勾選Use Motor
? Moter/Motor Speed = 500、Maximum Motor Force = 1000
? Ririgbody 2D
? 勾選Use Auto Mass
設置Wheel Joint 2D 2/3
63
? 調整truck到場景左側
? Position(X, Y, Z) = (-45, 0, 0)
? 調整Main Camera位置
? Position(X, Y, Z) = (-30.5, 0, 0)
? 測試遊戲,車子會跑出螢幕!!!
設置Wheel Joint 2D 3/3
64
? 在Main Camera新增CameraControl程式腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
public float xMargin = 1f; //攝影機啟動跟隨前允許玩家移動的水平位移
public float xSmooth = 2f; //使相機平穩地捕捉目標運動之X軸修正值
public float maxX; //攝影機位置X最大值
public float minX; //攝影機位置X最小值
private Transform player; //玩家角色transform屬性
void Awake () {
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void FixedUpdate () {
TrackPlayer();
}
攝影機追隨功能 1/2
65
bool CheckXMargin () {
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}
void TrackPlayer () {
float targetX = transform.position.x;
if (CheckXMargin())
targetX = Mathf.Lerp(transform.position.x, player.position.x,
xSmooth * Time.deltaTime);
targetX = Mathf.Clamp(targetX, minX, maxX);
transform.position = new Vector3(targetX,0, transform.position.z);
}
}
? 設定Main Camera之Camera Control
? Max X = 29.5,Min X = -29
? 測試遊戲,攝影機會追踨車子移動
攝影機追隨功能 2/2
66
? 拖曳Assets/Sprites/ChainLinksSprite_0.png場景
? Position(X, Y, Z) = (31.72, -2.68)
? Rotation(X, Y, Z) = (0, 0, 97)
? 加入Capsule Collider 2D
? 加入Hinge Joint 2D
? 取消勾選Auto Configure Connected Anchor
? Anchor(X, Y) = (0, 0.22)
? Connected Anchor(X, Y) = (31.5, -2.7)
使用Hinge Joint 2D建立鏈橋 1/9
67
? 拖曳Assets/Sprites/ChainLinksSprite_1.png場景
? Position(X, Y, Z) = (32.5, -2.58)
? Rotation(X, Y, Z) = (0, 0, 97)
? Order in Layer = 1
? 加入Capsule Collider 2D
? 加入Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_0
? 取消勾選Auto Configure Connected Anchor
? Anchor(X, Y) = (0, 0.34)
? Connected Anchor(X, Y) = (0, -0.43)
使用Hinge Joint 2D建立鏈橋 2/9
68
? Ctrl-D複製ChainLinksSprite_0
? Position(X, Y, Z) = (32.28, -2.49)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_1
? Anchor(X, Y) = (0, 0.37)
? Connected Anchor(X, Y) = (0, -0.4152)
? Ctrl-D複製ChainLinksSprite_1
? Position(X, Y, Z) = (34.03, -2.41)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_0(1)
使用Hinge Joint 2D建立鏈橋 3/9
69
? Ctrl-D複製ChainLinksSprite_0(1)
? Position(X, Y, Z) = (34.8, -2.31)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_1(1)
? Ctrl-D複製ChainLinksSprite_1(1)
? Position(X, Y, Z) = (35.56, -2.21)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_0(2)
使用Hinge Joint 2D建立鏈橋 4/9
70
? Ctrl-D複製ChainLinksSprite_0(2)
? Position(X, Y, Z) = (36.36, -2.12)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_1(2)
? Ctrl-D複製ChainLinksSprite_1(2)
? Position(X, Y, Z) = (37.13, -2.03)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_0(3)
使用Hinge Joint 2D建立鏈橋 5/9
71
? Ctrl-D複製ChainLinksSprite_0(3)
? Position(X, Y, Z) = (37.89, -1.93)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_1(3)
? Ctrl-D複製ChainLinksSprite_1(3)
? Position(X, Y, Z) = (38.67, -1.84)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_0(4)
使用Hinge Joint 2D建立鏈橋 6/9
72
? Ctrl-D複製ChainLinksSprite_0(4)
? Position(X, Y, Z) = (39.45, -1.76)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_1(4)
? 移除Capsule Collider 2D
? Ctrl-D複製ChainLinksSprite_1(4)
? Position(X, Y, Z) = (40.21, -1.67)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_0(5)
? 移除Capsule Collider 2D
使用Hinge Joint 2D建立鏈橋 7/9
73
? Ctrl-D複製ChainLinksSprite_0(5)
? Position(X, Y, Z) = (40.99, -1.58)
? Hinge Joint 2D
? Connected Rigid Body = ChainLinksSprite_1(5)
? 移除Capsule Collider 2D
? 加入Hinge Joint 2D
? 取消勾選Auto Configure Connected Anchor
? Anchor(X, Y) = (0, -0.33)
? Connected Anchor(X, Y) = (41.32, -1.53)
使用Hinge Joint 2D建立鏈橋 8/9
74
? 選單命令GameObject> Create Emepty,命名為Cable bridge
? 重置Transform
? 拖曳全部的ChainLinksSprite_XX成為Cable bridge之子物件
? 測試遊戲
使用Hinge Joint 2D建立鏈橋 9/9
75

More Related Content

Unity遊戲程式設計 - 2D 物理關節應用

  • 1. 2D 物理關節應用 Revised on December 25, 2019 ? Distance Joint ? Fixed Joint ? Friction Joint ? Hinge Joint ? Relative Joint ? 狠狠撸r Joint ? Spring Joint ? Target Joint ? Wheel Joint ? 綜合應用
  • 2. ? https://docs.unity3d.com/Manual/Joints2D.html ? 物理關節用來將遊戲物件與另?遊戲物件或場景空間的固定位置建 立物理連結 2D Joints 1/3 2 ? 適合用來建構場景上的機關陷阱 ? 套用Joint 2D之物件會自動加上 Rigidbody 2D性質
  • 3. ? 選單命令Component> Physical 2D子選單,可加入2D Joints元件 ? 距離關節 (Distance Joint 2D) 將兩個物體以固定相對距離方式連接 ? 固定關節 (Fixed Joint 2D) 使兩個物件維持固定的相對位置及角度,很類似階層裡的父子結構 ? 摩擦關節 (Friction Joint 2D) 在相連的兩個遊戲物件間加上摩擦力,使移動物件的直線速度及角速度減 慢至停止 ? 樞紐關節 (Hinge Joint 2D) 將剛體物件附接到它可以旋轉的?點。例如:?把剪?的樞軸,?旋轉軸 2D Joints 2/3 3
  • 4. ? 相對關節 (Relative Joint 2D) 將兩個剛體遊戲物件彼此保持在相同的相對位置。使用此關節可保持兩個 物體相互偏移。例如:在射擊遊戲中,玩家有額外的槍電池跟隨他們 ? 滑塊關節 (狠狠撸r Joint 2D) 限制遊戲物件沿著空間中的直線路徑滑動,例如滑動? ? 彈簧關節 (Spring Joint 2D) 將兩個剛體遊戲物件好像通過彈簧連接在?起 ? 目標關節 (Target Joint 2D) 類似相對關節,但連結具有彈性,用來利用回彈力將物體回到目標位置。 可用於拾取和移動例如在重力作用下的物體 ? 車輪接頭 (Wheel Joint 2D) 模擬車輪和懸架 2D Joints 3/3 4
  • 6. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body Distance Joint所要連接的另?物件,None表示要連接到世界座標 的定點(預設為原點) ? Auto Configure Connected Anchor 勾選時,會自動調整關節所連接物件之?點(Connected Anchor值) ? Anchor 關節?點座標(以圖片pivot為原點),預設值為物件中心點,也允許 於設在物件外部 ? Connected Anchor 關節在另?個物件的?點 Distance Joint 2D 2/4 6 Anchor Connected Anchor Distance Joint 2D
  • 7. ? Auto Configure Distance 自動調整關節距離(Distance值),預設為勾選 ? Distance Anchor到Connected Anchor之距離 ? Max Distance Only 只限制最大距離,連接的物件仍可彼此靠近,但不能超過Distance欄 設定值。若未勾選,則連接的兩物件永遠保持固定距離 Distance Joint 2D 3/4 7
  • 8. ? Break Force ? 斷開關節所需的最小力量,預設值為infinite ? 當關節的reactionForce值(只能在程式腳本讀取)高於breakForce,或 reactionTorque值(只能在程式腳本讀取)高於關節的breakTorque時, 關節將斷裂 ? 當關節斷裂時,系統會執行OnJointBreak2D事件程序,並且傳入 Joint2D。完成OnJointBreak2D事件程序後,關節將自動從遊戲物件中 移除並刪除 Distance Joint 2D 4/4 8
  • 9. ? 將Sprites/ball加入到場景 ? Position(X, Y, Z) = (-3, -3, 0) ? Scale(X, Y) = (0.5, 0.5) ? 在ball物件加入Distance Joint 2D元件 ? 選單命令Component> Physics 2D> Distance Joint 2D ? 取消勾選Auto Configure Distance ? 執行測試,ball會以場景中心為?點擺盪 Distance Joint 2D練習 1/3 9 rope end Anchor of Distance Joint 2D Distance Joint 2D Connected Anchor of Distance Joint 2D
  • 10. ? 將Sprite/rope end加入到場景 ? Position(X, Y, Z) = (-1, 0, 0) ? 在rope end物件加入運動剛體元件 ? 選單命令Component> Physics 2D> Rigidbody 2D ? Body Type = Kinematic ? 將rope end物件拖曳到ball之Distance Joint 2D的Connected Rigid Body屬性欄 ? 執行測試,ball會以rope end為?點擺盪 Distance Joint 2D練習 2/3 10 rope end Distance Joint 2D ball Connected Rigid Body
  • 11. ? 將ball移到rope end上方 ? Position(X, Y, Z) = (3, -3, 0) ? 勾選ball之Distance Joint 2D的Max Distance Only ? 執行測試,觀察ball運動情形 ? 反覆調整ball之Distance Joint 2D的Break Force值,執行測試, 觀察ball運動情形 Distance Joint 2D練習 3/3 11 rope end Distance Joint 2D ball Connected Rigid Body
  • 13. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?物件,None表示要連接到世界座標的定點 ? Auto Configure Connected Anchor 自動調整關節所連接物件之?點(Connected Anchor值) ? Anchor 關節?點,預設值為物件中心點,也允許於設在物件外部 ? Connected Anchor 關節在另?個物件的?點 Fixed Joint 2D 2/3 13
  • 14. ? Damping Ratio 阻尼比,0.0~1.0之間。阻尼比愈大,物件受力振動後愈快回復原狀 ? Frequency 物件振動頻率,0~1,000,000之間,數值愈大,物件愈不易來回振動 (0表示完全不振動) ? Break Force 斷開關節所需的最小力道 ? Break Torque 斷開關節所需的最小扭力 Fixed Joint 2D 3/3 14
  • 15. ? 將Sprites/ball加到場景 ? Scale(X, Y, Z) = (0.5, 0.5, 1) ? Position(X, Y, Z) = (-1.5, 3, 0) ? 在ball物件加入Rigidbody 2D元件 ? 選單命令Component> Physics 2D> Rigidbody 2D ? 在ball物件加入Circle Collider 2D元件 ? 選單命令Component> Physics 2D> Circle Collider 2D ? 將Sprites/plank加到場景,命名為plank a ? Position(X, Y, Z) = (0, 0, 0) ? 將Sprites/plank加到場景,命名為plank b ? Position(X, Y, Z) = (-2, -2, 0) Fixed Joint 2D練習 1/2 15
  • 16. ? 在plank a物件加入Box Collider 2D元件 ? 在plank a物件加入Fixed Joint 2D元件 ? Anchor(X, Y) = (1.35, 0) ? Connected Anchor(X, Y) = (1.35, 0) ? Frequency = 5 ? 在plank b物件加入Box Collider 2D元件 ? 在plank b物件加入Fixed Joint 2D元件 ? Anchor(X, Y) = (0, 0) ? Connected Anchor(X, Y) = (-2, -2) ? Frequency = 0.1 ? 執行測試,觀察ball掉落運動情形;調整Frequency及Damping Ratio,觀察plank振動情形 Fixed Joint 2D練習 2/2 16 ball ball Anchor of Fixed Joint 2D plank a plank b Anchor of Fixed Joint 2D
  • 18. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?物件,None表示要連接到世界座標的定點 ? Auto Configure Connected Anchor 自動調整關節所連接物件之?點(Connected Anchor值) ? Anchor 關節?點,預設為物件中心點,也允許於設在物件外部 ? Connected Anchor 關節在另?個物件的?點 Friction Joint 2D 2/3 18
  • 19. ? Max Force 設定直線移動摩擦力,預設值為1 ? Max Torque 設置旋轉摩擦力,預設值為1 ? Break Force 斷開關節所需的最小力道 ? Break Torque 斷開關節所需的最小扭力 Friction Joint 2D 3/3 19 upper plank
  • 20. ? 將Sprites/ball加入到場景 ? Position(X, Y, Z) = (-3, -1, 0) ? Scale(X, Y) = (0.5, 0.5) ? 在ball物件加入Distance Joint 2D元件 ? 選單命令Component> Physics 2D> Distance Joint 2D ? 執行測試,ball會以場景中心為?點擺盪 ? 在ball物件加入Friction Joint 2D ? 取消勾選Auto Configure Connected Anchor ? Max Force = 0.5,Max Torque = 0.5 ? Connected Anchor(X, Y) = (0, -4) ? 執行測試,ball是否較快停止擺盪 Friction Joint 2D練習 20 Distance Joint 2D Friction Joint 2D Connected Anchor Connected Anchor
  • 21. ? 樞紐關節可讓物件繞著轉軸旋轉 Hinge Joint 2D 1/3 21 Distance Joint 2DHinge Joint 2D
  • 22. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?剛體物件,None表示要連接到世界座標的定點 ? Auto Configure Connected Anchor 自動調整關節所連接到的物件之?點(Connected Anchor值) ? Anchor 剛體物體的旋轉點 ? Connected Anchor 剛體物體的旋轉點所附著的位置 Hinge Joint 2D 2/3 22
  • 23. ? Use Motor 勾選表示在樞紐關節加上馬達動力 ? Motor Speed 馬達速度,角度值/每秒,負值為逆時針 ? Maximum Motor Force 馬達最大扭力 ? Use Limits 勾選表示在樞紐關節加上旋轉角度限制 ? Lower Angle 樞紐旋轉角度下限(以Anchor為圓心) ? Upper Angle 樞紐旋轉角度上限(以Anchor為圓心) Hinge Joint 2D 3/3 23 Anchor Upper Angle Lower Angle
  • 24. ? 將Sprites/ball加到場景 ? Scale(X, Y, Z) = (0.5, 0.5, 1) ? Position(X, Y, Z) = (-2, 3, 0) ? 加入Circle Collider 2D元件 ? 選單命令Component> Physics 2D> Circle Collider 2D ? 加入Distance Joint 2D元件 ? 選單命令Component> Physics 2D> Distance Joint 2D ? 將Sprites/plank加到場景 ? Position(X, Y, Z) = (-5, 0, 0) ? 加入Box Collider 2D Hinge Joint 2D練習 1/2 24
  • 25. ? 在plank加上Hinge Joint 2D元件 ? Anchor(X, Y) = (-1, 0) ? Connected Anchor = (-6, 0) ? 勾選Use Motor ? Motor Speed:-30 ? Maximum Motor Force:15 ? 勾選Use Limits ? Lower Angle:0 ? Upper Angle:45 ? 執行測試,觀察plank活動情形 Hinge Joint 2D練習 2/2 25 Upper Angle Lower Angle Distance Joint 2D Anchor of Hinge Jointer 2D
  • 26. ? 使用Hinge Joint 2D建立鏈條 Hinge Joint 2D練習II 1/2 26
  • 29. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?剛體物件,None表示要連接到世界座標的定點 ? Max Force 關節連接力道,愈大值表示兩物件愈牢牢連接 ? Max Torque 關節連接扭力,愈大值表示兩物件愈牢牢連接 Relative Joint 2D 2/3 29
  • 30. ? Correction Scale 修正相對位置所施加的力道大小,0.0~1.0之間,預設值為0.3。力道 愈大,修正相對位置時擺盪情形愈明顯 ? Auto Configure Offset 自動調整關節所連接到的物件之距離偏移(Linear Offset)及角度偏 移(Angular Offset) ? Linear Offset 與關節所連接物件之距離偏移值 ? Angular Offset 與關節所連接物件之角度偏移值 Relative Joint 2D 3/3 30
  • 31. ? 將Sprites/plank加到場景 ? Position (X, Y, Z)=(0, -3.5, 0) ? Rotation (X, Y, Z)=(0, 0, -4) ? Scale (X, Y, Z)=(4, 1, 1) ? 加入Box Collider 2D ? 選單命令Component> Physics 2D> Box Collider 2D ? 將Sprites/ball加入到場景 ? Position(X, Y, Z) = (-4, 1, 0) ? Scale(X, Y, Z) = (0.3, 0.3, 1) ? 加入Rigidbody 2D ? 選單命令Component> Physics 2D> Rigidbody 2D Relative Joint 2D練習 1/2 31
  • 32. ? 將Sprites/ball加到場景,命名為ring ? Position (X, Y, Z) = (-4, 2, 0) ? Scale (X, Y, Z) = (0.15, 0.15, 1) ? 加入Circle Collider 2D元件 ? 加入Realtive Joint 2D元件 ? Connected Rigid Body = ball ? Max Force=100 ? Correction Scale = 0.1 ? 勾選Auto Configure Offset ? 複製7個ring物件,環繞ball成圓形 ? 執行測試,觀察ball及ring活動情形 Relative Joint 2D練習 2/2 32
  • 34. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?剛體物件,None表示要連接到世界座標的定點 ? Auto Configure Connected Anchor 自動調整關節所連接到的物件之?點(Connected Anchor值) ? Anchor 關節?點,預設值為物件中心點,也允許於設在物件外部 ? Connected Anchor 關節連接到場景上的?點 狠狠撸r Joint 2D 2/5 34
  • 35. ? Auto Configure Angle 自動調整與關節所連接物件之角度值(Angle) ? Angle 與關節所連接物件之角度值 ? Use Motor 勾選表示在關節施加馬達動力 ? Motor Speed 馬達速度 (公尺/秒),正值朝Lower Translation方向移動,負值朝 Upper Translation方向移動 ? Maximum Motor Force 馬達最大力道 狠狠撸r Joint 2D 3/5 35
  • 36. ? Use Limits 勾選表示啟用關節滑動距離限制 ? Lower Translation 滑動時Connected Anchor的下邊界位置(以Anchor值為原點) ? Upper Translation 滑動時Connected Anchor的上邊界位置(以Anchor值為原點) ? Break Force 斷裂力道,破壞關節所需的最小力量 ? Break Torque 斷裂扭力,破壞關節所需的最小扭力 狠狠撸r Joint 2D 4/5 36
  • 37. 狠狠撸r Joint 2D 5/5 37 當Motor speed為正值時 當Motor speed為負值時 Lower Translation Upper Translation Connected Anchor Connected Anchor Connected Anchor
  • 38. ? 將Sprites/ball加到場景 ? Scale(X, Y, Z) = (0.3, 0.3, 1) ? Position(X, Y, Z) = (-1.414, 1.414, 0) ? 加入Circle Collider 2D元件 ? 選單命令Component> Physics 2D> Circle Collider 2D ? 加入Distance Joint 2D元件 ? 選單命令Component> Physics 2D> Distance Joint 2D ? Break Force = 50 ? 將Sprites/plank加到場景 ? Position(X, Y, Z) = (1.5, -2, 0) ? 加入Box Collider 2D 狠狠撸r Joint 2D練習 1/2 38
  • 39. ? 在plank物件加入狠狠撸r Joint 2D ? 取消勾選Auto Configure Connected Anchor ? Anchor(X, Y) = (0, 0) ? Connected Anchor(X, Y) = (0, -2) ? Angle = 180 ? 勾選Use Motor ? Motor Speed:-1,Maximum Motor Force:2 ? 勾選Use Limits ? Lower Translation:1.5 ? Upper Translation:4.5 ? 執行測試,觀察plank活動情形 狠狠撸r Joint 2D練習 2/2 39 Upper Translation Connected Anchor AnchorLower Translatio n Distance Joint 2D
  • 41. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?剛體物件,None表示要連接到世界座標的定點 ? Auto Configure Connected Anchor 自動調整關節所連接到的物件之?點(Connected Anchor值) ? Anchor 關節?點,預設值為物件中心點,也允許於設在物件外部 ? Connected Anchor 關節在另?個物件的?點 Spring Joint 2D 2/3 41
  • 42. ? Auto Configure Distance 自動調整Distance值。要有彈簧效果,不可勾選此項 ? Distance 彈簧?度 ? Damping Ratio 阻尼比,0.0~1.0之間。阻尼比愈大,彈簧愈快回復靜止狀態 ? Frequency 彈簧振動的頻率,0~1,000,000之間,值愈大,表示彈簧愈硬 Spring Joint 2D 3/3 42
  • 43. ? 將Sprites/plank加到場景 ? Position(X, Y, Z) = (0, 3, 0) ? 加入Rigidbody 2D ? Body Type = Kinematic ? 將Sprites/ball加到場景 ? Position(X, Y, Z) = (0, -2, 0) ? Scale(X, Y, Z) = (0.4, 04, 0) ? 加入Spring Joint 2D ? Connected Rigid Body = plank ? 取消勾選Auto Configure Distance ? 執行測試,使用滑鼠拖曳ball再放開,觀察ball擺盪情形 Spring Joint 2D練習 43 Connected Rigid Body Spring Joint 2D Distance Anchor
  • 45. ? Anchor 關節?點,預設值為物件中心點,也允許於設在物件外部 ? Target 物體的目標點 ? Auto Configure Target 自動調整Target值 ? Max Force 朝向目標點移動的力道 ? Damping Ratio 阻尼比,0.0~1.0之間,阻尼比愈大表示來回運動次數愈少 Target Joint 2D 2/3 45
  • 46. ? Frequency 振動的頻率,0~1,000,000之間,值愈大,表示彈簧愈硬 ? Break Force 斷裂力道,破壞關節所需的最小力量 Target Joint 2D 3/3 46
  • 47. ? 將Sprites/ball加到場景 ? Position (X, Y, Z) = (-7.5, 4, 0) ? Scale (X, Y, Z) = (0.4, 0.4, 1) ? 加入Rigidbody 2D ? 加入Circle Collider 2D ? 將Sprites/plank加到場景 ? Position (X, Y, Z) = (-8, 2, 0) ? Rotation (X, Y, Z) = (0, 0, -1) ? Scale (X, Y, Z) = (0.6, 1, 1) ? 加入Rigidbody 2D ? 勾選Freeze Rotation Z ? 加入Box Collider 2D Target Joint 2D練習 1/2 47
  • 48. ? 加入Target Joint 2D ? 勾選Auto Configure Target ? Max Force = 30 ? Damping Ratio = 0.1 ? Frequency = 3 ? 複製6個plank物件,排列成階梯狀;執行測試,觀察執行結果 Target Joint 2D練習 2/2 48
  • 50. ? Enable Collision 啟用關節所連接的兩個物件間的碰撞偵測 ? Connected Rigid Body 關節所要連接的另?剛體物件,None表示要連接到世界座標的定點 ? Auto Configure Connected Anchor 自動調整關節所連接到的物件之?點(Connected Anchor值) ? Anchor 關節?點,預設值為物件中心點,也允許於設在物件外部 ? Connected Anchor 關節在另?個物件的?點 Wheel Joint 2D 2/3 50
  • 51. ? Suspension 車輪懸吊系統設定 ? Damping Ratio 阻尼比,0.0~1.0之間,阻尼比愈大表示彈簧來回運動次數愈少 ? Frequency 彈簧振動的頻率,0~1,000,000之間,值愈大,表示彈簧愈硬 ? Angle 彈簧懸吊角度 ? Use Motor 施加馬達動力 ? Motor Speed 馬達速度 (角度值/秒) ? Maximum Motor Force 馬達最大力道 Wheel Joint 2D 3/3 51
  • 52. ? 將Sprites/plank加到場景 ? Position(X, Y, Z) = (-7.5, 0, 0) ? 加入Rigidbody 2D ? 將Sprites/car wheel加到場景 ? Position(X, Y, Z) = (-8.5, -1.2, 0) ? Scale(X, Y, Z) = (0.5, 0.5, 1) ? 加入Wheel Joint 2D ? Connected Rigid Body = plank ? 取消勾選Auto Configure Connected Anchor ? Anchor(X, Y) = (0, 0) ? Connected Anchor(X, Y) = (-1, -1.25) Wheel Joint 2D練習 1/2 52
  • 53. ? 勾選Use Motor ? Motor Speed = 10 ? 加入Circle Collider 2D ? 複製car wheel ? Position(X, Y, Z) = (-6.5, -1.25, 0) ? Wheel Joint 2D ? Connected Anchor(X, Y) = (1, -1.25) ? 將Sprites/plank加到場景 ? Position(X, Y, Z) = (0, -3, 0) ? Scale(X, Y, Z) = (7, 1, 1) ? 加入Box Collider 2D Wheel Joint 2D練習 2/2 53
  • 55. ? 設定Main Camera ? Size = 10 ? 將background加入場景 ? Position(X, Y, Z) = (0, 0, 0) ? Scale(X, Y, Z) = (5, 5, 1) ? Order in Layer = -1 建置場景 1/2 55
  • 56. ? 在background加入Edge Collider 2D ? Edge Radius = 0.1 ? 編輯Edge Collier線段,使其符合背景地形 建置場景 2/2 56 Edge Collider 2D
  • 57. ? 將car body加到場景,命名為truck ? 重置Transform ? Order in Layer = 1 ? tag = Player ? 將suspension加到場景,命名為front suspension ? Position(X, Y, Z) = (3.77, -0.3, 0) ? Scale(X, Y, Z) = (1.5, 1, 1) ? Order in Layer = 2 ? 將suspension加到場景,命名為rear suspension ? Position(X, Y, Z) = (-3.25, -0.3, 0) ? Scale(X, Y, Z) = (1.5, 1, 1) ? Order in Layer = 2 組合卡車 1/3 57
  • 58. ? 將car wheel加到場景,命名為front wheel ? Position(X, Y, Z) = (3.82, -1.85, 0) ? Scale(X, Y, Z) = (1.3, 1.3, 1) ? Order in Layer = 3 ? 將car wheel加到場景,命名為rear wheel ? Position(X, Y, Z) = (-3.28, -1.9, 0) ? Scale(X, Y, Z) = (1.3, 1.3, 1) ? Order in Layer = 3 組合卡車 2/3 58
  • 59. ? 將front suspension、rear suspension、front wheel及rear wheel拖曳成為turck子物件 ? 調整turck尺寸 ? Scale(X, Y, Z) = (0.7, 0.7, 1) 組合卡車 3/3 59
  • 60. ? truck加入Rigidbody 2D ? 取消勾選Use Auto Mass ? Mass = 5 ? truck加入Polygon Collider 2D ? 編輯Polygon Collider,僅量用較少的端點 來覆蓋車體外形 ? 保留點數最多的Element ? ctrl-click刪除端點 設置卡車碰撞器 1/2 60 Circle Collider 2D Edge Collider 2D
  • 61. ? 在front wheel及rear wheel加入Circle Collider 2D 設置卡車碰撞器 2/2 61
  • 62. ? front wheel加入Wheel Joint 2D ? Connected Rigid Body = truck ? 取消勾選Auto Configure Connected Anchor ? Connected Anchor(X, Y) = (3.82, -1.85) ? Suspension/Damping Ratio = 0.7、Frequency = 10、Angle = 90 ? 勾選Use Motor ? Moter/Motor Speed = 600、Maximum Motor Force = 1000 ? Ririgbody 2D ? 勾選Use Auto Mass 設置Wheel Joint 2D 1/3 62
  • 63. ? rear wheel加入Wheel Joint 2D ? Connected Rigid Body = truck ? 取消勾選Auto Configure Connected Anchor ? Connected Anchor(X, Y) = (-3.28, -1.9) ? Suspension/Damping Ratio = 0.7、Frequency = 10、Angle = 90 ? 勾選Use Motor ? Moter/Motor Speed = 500、Maximum Motor Force = 1000 ? Ririgbody 2D ? 勾選Use Auto Mass 設置Wheel Joint 2D 2/3 63
  • 64. ? 調整truck到場景左側 ? Position(X, Y, Z) = (-45, 0, 0) ? 調整Main Camera位置 ? Position(X, Y, Z) = (-30.5, 0, 0) ? 測試遊戲,車子會跑出螢幕!!! 設置Wheel Joint 2D 3/3 64
  • 65. ? 在Main Camera新增CameraControl程式腳本 using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraControl : MonoBehaviour { public float xMargin = 1f; //攝影機啟動跟隨前允許玩家移動的水平位移 public float xSmooth = 2f; //使相機平穩地捕捉目標運動之X軸修正值 public float maxX; //攝影機位置X最大值 public float minX; //攝影機位置X最小值 private Transform player; //玩家角色transform屬性 void Awake () { player = GameObject.FindGameObjectWithTag("Player").transform; } void FixedUpdate () { TrackPlayer(); } 攝影機追隨功能 1/2 65
  • 66. bool CheckXMargin () { return Mathf.Abs(transform.position.x - player.position.x) > xMargin; } void TrackPlayer () { float targetX = transform.position.x; if (CheckXMargin()) targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime); targetX = Mathf.Clamp(targetX, minX, maxX); transform.position = new Vector3(targetX,0, transform.position.z); } } ? 設定Main Camera之Camera Control ? Max X = 29.5,Min X = -29 ? 測試遊戲,攝影機會追踨車子移動 攝影機追隨功能 2/2 66
  • 67. ? 拖曳Assets/Sprites/ChainLinksSprite_0.png場景 ? Position(X, Y, Z) = (31.72, -2.68) ? Rotation(X, Y, Z) = (0, 0, 97) ? 加入Capsule Collider 2D ? 加入Hinge Joint 2D ? 取消勾選Auto Configure Connected Anchor ? Anchor(X, Y) = (0, 0.22) ? Connected Anchor(X, Y) = (31.5, -2.7) 使用Hinge Joint 2D建立鏈橋 1/9 67
  • 68. ? 拖曳Assets/Sprites/ChainLinksSprite_1.png場景 ? Position(X, Y, Z) = (32.5, -2.58) ? Rotation(X, Y, Z) = (0, 0, 97) ? Order in Layer = 1 ? 加入Capsule Collider 2D ? 加入Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_0 ? 取消勾選Auto Configure Connected Anchor ? Anchor(X, Y) = (0, 0.34) ? Connected Anchor(X, Y) = (0, -0.43) 使用Hinge Joint 2D建立鏈橋 2/9 68
  • 69. ? Ctrl-D複製ChainLinksSprite_0 ? Position(X, Y, Z) = (32.28, -2.49) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_1 ? Anchor(X, Y) = (0, 0.37) ? Connected Anchor(X, Y) = (0, -0.4152) ? Ctrl-D複製ChainLinksSprite_1 ? Position(X, Y, Z) = (34.03, -2.41) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_0(1) 使用Hinge Joint 2D建立鏈橋 3/9 69
  • 70. ? Ctrl-D複製ChainLinksSprite_0(1) ? Position(X, Y, Z) = (34.8, -2.31) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_1(1) ? Ctrl-D複製ChainLinksSprite_1(1) ? Position(X, Y, Z) = (35.56, -2.21) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_0(2) 使用Hinge Joint 2D建立鏈橋 4/9 70
  • 71. ? Ctrl-D複製ChainLinksSprite_0(2) ? Position(X, Y, Z) = (36.36, -2.12) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_1(2) ? Ctrl-D複製ChainLinksSprite_1(2) ? Position(X, Y, Z) = (37.13, -2.03) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_0(3) 使用Hinge Joint 2D建立鏈橋 5/9 71
  • 72. ? Ctrl-D複製ChainLinksSprite_0(3) ? Position(X, Y, Z) = (37.89, -1.93) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_1(3) ? Ctrl-D複製ChainLinksSprite_1(3) ? Position(X, Y, Z) = (38.67, -1.84) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_0(4) 使用Hinge Joint 2D建立鏈橋 6/9 72
  • 73. ? Ctrl-D複製ChainLinksSprite_0(4) ? Position(X, Y, Z) = (39.45, -1.76) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_1(4) ? 移除Capsule Collider 2D ? Ctrl-D複製ChainLinksSprite_1(4) ? Position(X, Y, Z) = (40.21, -1.67) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_0(5) ? 移除Capsule Collider 2D 使用Hinge Joint 2D建立鏈橋 7/9 73
  • 74. ? Ctrl-D複製ChainLinksSprite_0(5) ? Position(X, Y, Z) = (40.99, -1.58) ? Hinge Joint 2D ? Connected Rigid Body = ChainLinksSprite_1(5) ? 移除Capsule Collider 2D ? 加入Hinge Joint 2D ? 取消勾選Auto Configure Connected Anchor ? Anchor(X, Y) = (0, -0.33) ? Connected Anchor(X, Y) = (41.32, -1.53) 使用Hinge Joint 2D建立鏈橋 8/9 74
  • 75. ? 選單命令GameObject> Create Emepty,命名為Cable bridge ? 重置Transform ? 拖曳全部的ChainLinksSprite_XX成為Cable bridge之子物件 ? 測試遊戲 使用Hinge Joint 2D建立鏈橋 9/9 75