Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Unity遊戲程式設計(04) 2D移動與碰撞處理I
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Wu, ShyiShiou
November 14, 2016
Programming
1.5k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Unity遊戲程式設計(04) 2D移動與碰撞處理I
介紹如何使用碰撞器及剛體物件
Wu, ShyiShiou
November 14, 2016
More Decks by Wu, ShyiShiou
See All by Wu, ShyiShiou
使用Evernote雲端筆記本
sswu
0
88
mBot教學(6)光感測器應用
sswu
0
790
Unity遊戲程式設計(01) Unity簡介
sswu
0
540
Unity遊戲程式設計(02) 應用2D圖片物件
sswu
0
1.2k
Unity遊戲程式設計(03) 製作及應用2D動畫
sswu
0
1.5k
Other Decks in Programming
See All in Programming
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
2
350
AIエージェント時代のコードレビューを設計する
nogu66
6
2.5k
Building an Out-of-Order CPU
latte72
0
700
新卒PdEのリアル
ryu1013
1
410
自分的「カンファレンスの楽しみ方」
syumai
0
190
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
270
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
1
500
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
120
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
190
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.8k
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
3
310
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.6k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
510
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
430
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
Believing is Seeing
oripsolob
1
210
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
250
Transcript
電子工程系應 用 電 子 組 電 腦 遊 戲 設
計 組 Unity遊戲程式設計(04) 2D移動與碰撞處理I 吳錫修 Nov 14, 2016
shape the future 物理世界兩個物體撞在⼀起會發⽣碰撞,遊戲世界裡要透過碰撞器 來模擬現實世界的碰撞情形 碰撞器是在元件加入⼀層透明的偵測層,代表元件物理效應發⽣的 範圍
選單「Component/Physics 2D」內可找到所有碰撞器 Rigidbody 2D (剛體) 埸景中會移動的元件都應加上剛體 具備物理性質:摩擦力 (Mass)、線性阻力 ( Linear Drag)、旋轉阻力 (Angular Drag)、重力 (Gravity Scale) IsKinematic屬性 設定為運動物件,本身不受物理性質影響,但會受其它物件影響 碰撞器 1/3 2 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future Box Collider 2D (方框碰撞器) 適用於矩形物體
Circle Collider 2D (圓形碰撞器) 適用於球形物體 Edge Collider 2D (邊緣碰撞器) 適用於邊界、地板或天花板 可編輯/調整線段節點 Polygon Collider 2D (多邊形碰撞器) 能較精準貼近不規則物體,但會耗用較多運算資源 碰撞器 2/3 3 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future Is Trigger屬性 通常道具元件會勾選Is Trigger,具碰撞檢測功能並發送碰撞通知
主角可穿透被設定為Is Trigger之道具 剛體元件程式腳本使用下列函式處理碰撞 void OnTriggerEnter2D(Collider2D other) { //進入碰接撞時 } void OnTriggerExit2D(Collider2D other) { //結束碰接撞時 } 碰撞器 3/3 4 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 在Update()中控制物件移動 更新座標方式 (要計算位移,物體移動比較⽣硬不自然) transform.Translate(Vector3.up *
0.1f); transform.Translate(Vector3.down * 0.1f); transform.Translate(Vector3.right * 0.1f); transform.Translate(Vector3.left * 0.1f); 對剛體物件施力 _rigidbody2D = this.GetComponent<Rigidbody2D>(); Vector2 force2D = Vector2.zero; force2D.y += forceValue; // force2D.x += forceValue; _rigidbody2D.AddForce(force2D); 物件移動控制 5 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 新增2D專案 將預設場景以ufo名稱存檔 將ufo.png、background.png、gold.png素材滙入到專案 Assets\Sprite資料夾
UFO遊戲設計 6 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 將background拖曳到ufo場景 Position設定為 (x, y, z)
= (0, 0, 0) Main Camera元件 將Camera Background設定為黑色 將Camera Size設定為16.5 設定場景背景 7 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 將ufo拖曳到ufo場景 Position設定為 (x, y, z)
= (0, 0, 0) 加入UFO物件 8 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 為ufo元件加入UFOController程式腳本 讓UFO動起來 1/3 9 Wu, ShyiShiou
Dept. of E.E., NKUT
shape the future 編輯UFOController程式如下: public float speed; void Update
() { if (Input.GetKey(KeyCode.W)){ this.transform.position += new Vector3 (0, speed*Time.deltaTime, 0); } if (Input.GetKey (KeyCode.S)) { this.transform.position += new Vector3 (0, -speed*Time.deltaTime, 0); } if (Input.GetKey(KeyCode.A)) { this.transform.position += new Vector3 (-speed*Time.deltaTime, 0, 0); } if (Input.GetKey(KeyCode.D)){ this.transform.position += new Vector3 (speed*Time.deltaTime, 0, 0); } } 讓UFO動起來 2/3 10 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future Inspector面板將ufo之speed設定為10,執行測試看看 讓UFO動起來 3/3 11 Wu, ShyiShiou
Dept. of E.E., NKUT 可以用W、A、S、D鍵移動飛碟 飛碟移動起並不自然 飛碟可以飛出邊界
shape the future 選取ufo元件 Component選單Physics 2D\Rigidbody 2D,加入剛體性質
執行測試發現ufo會自動往下掉; 把Gravity Scale改成0,模擬太空無重力 狀態 改用物理加速度方式移動 1/3 12 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 修改UFOController程式如下: public float forceValue; private Rigidbody2D
_rigidbody2D = null; void Start () { _rigidbody2D = this.GetComponent<Rigidbody2D> (); } void Update () { Vector2 force2D = Vector2.zero; if (Input.GetKey(KeyCode.W)){ force2D.y += forceValue; } if (Input.GetKey (KeyCode.S)) { force2D.y -= forceValue; } 改用物理加速度方式移動 2/3 13 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future if (Input.GetKey(KeyCode.A)) { force2D.x -= forceValue; }
if (Input.GetKey(KeyCode.D)){ force2D.x += forceValue; } _rigidbody2D.AddForce(force2D); } Force Value設定為3,執行測試看看 飛碟會以加速度移動了 飛碟還是會飛出邊界 改用物理加速度方式移動 3/3 14 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 選取ufo元件 Component選單Physics 2D\Circle Collider 2D,加入圓形碰撞器
把Radius調成2.12,讓Collider2D大小 與ufo圖片重合 設定UFO飛行邊界 1/2 15 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 選取background元件 Component選單Physics 2D\Edge Collider 2D,加入邊緣碰撞器
點擊「Edit Collider」按鈕 讓edit collider環繞背景內緣四週 執行測試,飛碟不會穿牆出去了 設定UFO飛行邊界 2/2 16 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 在Hierarchy面板中,將 Main Camera拖到ufo元件底下 執行測試
遊戲鏡頭會以飛碟為中心 飛碟撞到邊界,飛碟會旋轉;由於Camera鏡頭是跟附在飛碟上,飛碟 時旋轉遊戲鏡頭也會跟著旋轉,背景圖就會旋轉 讓Camera鏡頭跟著飛碟移動 1/2 17 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 為Main Camera元件加入CameraController程式腳本,關閉鏡頭旋 轉,程式如下 public class CameraController
: MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { this.transform.eulerAngles = Vector3.zero; } } 執行測試 飛碟可以在邊界內飛行,背景圖也不會旋轉了 讓Camera鏡頭跟著飛碟移動 2/2 18 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 拖曳gold.png到場景 命名為pickup Component選單Physics 2D\Rigidbody
2D,加入剛體性質 勾選Is Kinematic 佈置金塊道具 1/5 19 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future Component選單Physics 2D\Circle Collider 2D,加入圓形碰撞器 勾選Is
Trigger 佈置金塊道具 2/5 20 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 為pickup元件加入PickupController程式腳本,讓金塊自主旋轉 public class PickupController : MonoBehaviour
{ public float RotateSpeed; // Use this for initialization void Start () { } // Update is called once per frame void Update () { this.transform.Rotate (new Vector3(0,0,RotateSpeed*Time.deltaTime)); } } 佈置金塊道具 3/5 21 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future Inspector面板,Rotate Speed設定為45 佈置金塊道具 4/5 22 Wu,
ShyiShiou Dept. of E.E., NKUT
shape the future 從Hierarchy面板將pickup拖曳到Assets/Prefab資料夾,建立pickup 預製元件 從Assets/Prefab資料夾拖曳pickup預製元件到場景,擺設如下 測試看看
飛碟會穿過金塊 佈置金塊道具 5/5 23 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 當飛碟碰到金塊,讓金塊消失 開啟UFOController.cs,加入以下程式碼 void OnTriggerEnter2D(Collider2D other)
{ other.gameObject.SetActive (false); } void OnTriggerExit2D(Collider2D other) { } 執行測試 飛碟碰到金塊時,金塊會自動消失了 讓UFO收集金塊 24 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future GameObject->UI->Text命令加入⼀個Text元 件,命名為Score Anchor設為左上角 Pos
X設為40,Pos Y設為-25 Width設為200,Height設為48 Text設為Score: 0 Font Size設為32 Color設為白色 顯示分數 1/6 25 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 新增UI元件時,會自動建立Canvas元件,而如果場景中已經有畫布, 新增的 UI 元素將會自動成為此畫布的子物件 遊戲執行時,Canvas會依據Render
Mode自動調整Canvas大小及 位置 顯示分數 2/6 26 Wu, ShyiShiou Dept. of E.E., NKUT Canvas Game resolution
shape the future Score元件加入ScoreManager程式腳本,程式如下 using UnityEngine; using System.Collections; using
UnityEngine.UI; public class ScoreManager : MonoBehaviour { private Text _text; private float _currentScore = 0; const string ScorePrefix = "Score : "; // Use this for initialization void Start () { _text = this.GetComponent<Text> (); _text.text = ScorePrefix + _currentScore; } 顯示分數 3/6 27 Wu, ShyiShiou Dept. of E.E., NKUT 注意Text是定義在Unity.Engine.UI底下
shape the future public void AddScore(int score) { _currentScore +=
score; _text.text = ScorePrefix + _currentScore; } // Update is called once per frame void Update () { } } 顯示分數 4/6 28 Wu, ShyiShiou Dept. of E.E., NKUT
shape the future 修改UFOController.cs,當飛碟收集到金塊時增加分數 using UnityEngine; using System.Collections; public
class UFOController : MonoBehaviour { public float forceValue; private Rigidbody2D _rigidbody2D = null; public ScoreManager scoremanager; ... void OnTriggerEnter2D(Collider2D other) { other.gameObject.SetActive (false); scoremanager.AddScore (100); } } 顯示分數 5/6 29 Wu, ShyiShiou Dept. of E.E., NKUT 宣告ScoreManager 更新分數
shape the future 選取ufo物件,然後把Score拖進ScoreManager的欄位 顯示分數 6/6 30 Wu, ShyiShiou
Dept. of E.E., NKUT