Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Unity遊戲程式設計(04) 2D移動與碰撞處理I
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.3k
最近”作らせてる” ローカルAIアプリの話
akit37
0
480
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
5.2k
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
17k
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
170
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
0
100
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2.5k
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
170
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
ActionController meets Protocol Buffers
sorah
0
560
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
150
Featured
See All Featured
Accessibility Awareness
sabderemane
1
250
A designer walks into a library…
pauljervisheath
211
25k
Test your architecture with Archunit
thirion
2
2.4k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
640
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
480
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
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