Weitere ähnliche Inhalte Ähnlich wie Unity遊戲程式設計 - 2D運動與碰撞處理I (20) Mehr von 吳錫修 (ShyiShiou Wu) (20) Unity遊戲程式設計 - 2D運動與碰撞處理I3. Box Collider 2D(方框碰撞器)
適用於矩形物體
Circle Collider 2D(圓形碰撞器)
適用於圓形物體
Edge Collider 2D(邊緣碰撞器)
適用於邊界、地板或天花板
可編輯/調整線段節點
即使將它編輯成封閉迴路,也只有在線段上才會碰撞,內部不會產⽣碰撞
碰撞器 2/5
3
Circle Collider Box Collider
4. Polygon Collider 2D(多邊形碰撞器)
能較精準貼近不規則物體,但會耗用較多運算資源
Capsule Collider 2D(膠囊碰撞器)
適用於膠囊形狀物體
Composite Collider 2D(複合碰撞器)
用來將子物件的碰撞器(Box Collider 2D及Polygon Collider 2D)合
併成⼀個碰撞器
子物件勾選Used By Composite
會自動附加Rigidbody 2D元件
碰撞器 3/5
4
Polygon ColliderCapsule Collider
7. 當碰撞器被設定Is Trigger屬性,即成為觸發器
其它碰撞器物件可以穿透觸發器物件
通常道具元件會設定為Is Trigger
在物件之程式腳本可使用下列函式來處理碰撞事件
void OnTriggerEnter2D(Collider2D other) {
//進入碰撞範圍時
}
void OnTriggerStay2D(Collider2D other) {
//在碰撞物上時
}
void OnTriggerExit2D(Collider2D other) {
//脫離碰撞範圍時
}
觸發器
7
8. 剛體是具物理性質的元件,可使物件在物理引擎控制下擬真運動
物理性質:質量(Mass)、移動阻力(Linear Drag)、旋轉阻力(Angular
Drag)、重力(Gravity Scale)
套用剛體元件可方便快速建立埸景中的運動物件
Body Type決定剛體的運動及碰撞互動特性
Dynamic
動態剛體完全模擬真實世界物理性質,物件會受物理作用力移動。動態剛
體會與其他剛體碰撞,是最耗效能的剛體類型
Kinematic
運動剛體不受到重力和力的影響,對系統資源的要求較低,常用來製作移
動式平台
Static
靜態剛體是⼀個固定不動物體,最不耗用系統資源
剛體 (Rigid body)
8
9. 動態剛體屬性
Material
物件材質
Simulated
啟用物理引擎模擬剛體功能
Use Auto Mass
依據元件的碰撞器自動計算物體質量
Mass
物體質量
Linear Drag
物體移動的阻力係數
Angular Drag
物體旋轉的阻力係數
2D剛體 (Rigidbody 2D) 1/4
9
10. Gravity Scale
物體受重力影響的程度
Collision Detection
碰撞檢測機制
Discrete
新位置產⽣時才檢測;如果物體移動速度太快,在物理更新期間,剛體
可能與碰撞器重疊或穿過
Continuous
Unity先計算新位置座標是否碰撞再移動剛體。會耗用較多運算資源
Sleeping Mode
物體休眠模式,讓靜止的物體進入休眠以減低運算資源;例如箱子掉落到
地板後進入休眠
Never Sleep
禁用休眠 (會耗用較多運算資源,儘可能不用)
2D剛體 (Rigidbody 2D) 2/4
10
11. Start Awake
初始為活躍狀態
Start Asleep
初始為休眠,但可以被碰撞喚醒
Interpolate
在物理更新之間進行插值方式 (當運動趨於劇烈時有用)
None
Interpolate
基於前⼀幀中的位置座標計算插值使運動平滑
Extrapolate
基於下⼀幀中的位置座標計算插值使運動平滑
Constraints
設定剛體移動或旋轉之限制
2D剛體 (Rigidbody 2D) 3/4
11
12. 運動剛體屬性
Use Full Kinematic Contacts
勾選時,運動剛體能與所有剛體產⽣碰撞;否則運動剛體只會與動態剛體
產⽣碰撞
其餘屬性參照動態剛體屬性說明
靜態剛體屬性
2D剛體 (Rigidbody 2D) 4/4
12
13. 更新座標方式(要計算位移,物體移動比較⽣硬不自然)
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);
物件移動控制
13
18. 編輯UFOController程式如下:
[SerializeField]
private 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
18
20. 在UFO元件加上剛體性質
選取UFO元件
選單命令Component> Physics 2D> Rigidbody 2D
執行測試發現UFO會受重力而往下墬落
把Gravity Scale改成0,模擬太空無重力狀態
改用物理加速度方式移動 1/3
20
21. 修改UFOController程式如下:
[SerializeField]
private float speed;
private Rigidbody2D rigidbody2D = null;
void Start () {
rigidbody2D = this.GetComponent<Rigidbody2D> ();
}
void FixedUpdate() {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
rigidbody2D.AddForce (movement * speed);
}
改用物理加速度方式移動 2/3
21
29. Pickups元件加入PickupController程式腳本,讓金塊自主旋轉
public class PickupController : MonoBehaviour {
[SerializeField]
private 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
29
33. 選單命令GameObject> UI> Text新增Text元件
,命名為CountText
Anchor設為左上角 (Pos屬性的參考原點)
Pos(X,Y,Z) = (10, -10, 0) (依實際情形調整)
Width設為100,Height設為100
Pivot(X,Y) = (0, 1)
Text = CountText
Font Size設為14
Color設為黃色
顯示遊戲訊息 1/6
33
34. 選單命令GameObject> UI> Text新增Text元件
,命名為WinText
Anchor設為中心
Pos(X,Y,Z) = (0, 75, 0) (依實際情形調整)
Width設為160,Height設為30
Text設為WinText
Alignment設為置中
Font Size設為24
Color設為黃色
顯示遊戲訊息 2/6
34
36. 修改UFOController程式腳本,當飛碟收集到金塊時更新CountText
訊息,收集滿12個金塊時顯示You win!
using UnityEngine;
using UnityEngine.UI;
public class UFOController : MonoBehaviour {
[SerializeField]
private float speed;
private Rigidbody2D rigidbody2D = null;
[SerializeField]
private Text countText;
[SerializeField]
private Text winText;
private int count;
void Start () {
rigidbody2D = this.GetComponent<Rigidbody2D> ();
count = 0;
winText.text = "";
SetCountText ();
}
顯示遊戲訊息 4/6
36
注意Text是定義在Unity.Engine.UI底下
37. void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.CompareTag ("PickUp")) {
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
}
}
void SetCountText() {
countText.text = "Count: " + _count.ToString ();
if (count >= 12)
winText.text = "You win!";
}
}
顯示遊戲訊息 5/6
37
40. 在Main Camera新增以下程式腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UpdateAndFixedUpdate : MonoBehaviour {
void Update () {
Debug.Log ("Update time: " + Time.deltaTime);
}
void FixedUpdate(){
Debug.Log ("FixedUpdate time: " + Time.deltaTime);
}
}
Update()與FixedUpdate() 2/3
40