###### tags: `Unity Learning`
# Unity 3D
[toc]
# 移動
### Transform
translate 物體移動
position 物體x,y,z位置 
---
### 物體的移動
vector3.forward --> vector3(0,0,1);.
vector3.up --> vector3(0,1,0);
vector3.right --> vector(1,0,0);
``` csharp
transform.translate(vector3.forward * Time.deltaTime * 整數);
//物件往以Z軸每單位為每秒1 meter且速度為 (整數) 前進
```
---
# 讀使用者輸入哪顆鍵
Input.GetKeyDown(KeyCode.按鍵)
Input.GetKey(KeyCode.按鍵)
```csharp=
Input.GetKeyDown(KeyCode.Space)
//讀使用者輸入空白鍵
```
---
# 複製已存在的物件
Instantiate(物件(Prefabs), 物件生成位置,物件生成角度);

---
# 宣告Private變數的抓物件方法
[參考連結](https://theunity3d.blogspot.com/2012/04/find.html)
- 找物件
```csharp=
GameObject.Find("name"); //用名字尋找GameObject子物件
GameObject.FindWithTag("Tag"); //用Tag尋找物件(第一個物件)
GameObject.FindGameObjectsWithTag("Tag"); //用Tag尋找物件(全部物件)
Resources.FindObjectsOfTypeAll(typeof(colliderTest)) //尋找Objects
```
---
# 刪除物件
Destroy(gameObject);
---
# 每隔一段時間就呼叫function
<span style="color:red;"> **需先自訂一個function**</span>
InvokeRepeating("function_name",float 開始時間,float 間隔);
---
# Box collider
先拉好碰撞範圍
### Collision 碰撞
造成物理碰撞,可以在碰撞時執行OnCollision函式。
最重要的核心!只要有碰撞器就會對其他物件產生碰撞,
若自身要受到碰撞的話,則需要碰撞器+剛體,缺一不可。
若自身要受到程式Translate位移或Rotate旋轉,也至少需要碰撞器。
### is Trigger 觸碰
Trigger觸發,取消所有物理碰撞,可以在觸發時執行OnTrigger函式。勾了就不允許自身受到/造成碰撞,也不會受到重力等物理作用力影響,
而是改為Trigger觸發,雙方碰到時會直接穿越並執行Trigger函式。
但自身還是可以受到程式位移或旋轉。
### 「碰撞」的Function
trigger可以穿過去,collider則無法穿過去。
- 走進碰撞範圍的一剎那
``` csharp
void OnTriggerEnter(Collider variable) {}
void OnCollisionEnter(Collision collision){}
```
- 持續停留在碰撞範圍
``` csharp
void OnTriggerStay(Collider variable) {}
void OnCollisionStay(Collision collision){}
```
- 離開碰撞範圍
``` csharp
void OnTriggerExit(Collider variable) {}
void OnCollisionExit(Collision collision){}
```
#### 當有多個物件需要被分開處理時,可以使用TAG來偵測不同物件
1. 新增TAG


2. 利用tag來偵測不同物件,進行不同處理
使用到
```csharp=
Collision_variable.gameObject.CompareTag("tag_name")
```
```csharp=
void OnCollisionEnter(Collision collision){ //碰撞發生
if(collision.gameObject.CompareTag("Ground"))
{
//當碰撞發生的物體的tag為Ground則執行block裡的內容
}
else if(collision.gameObject.CompareTag("Obstacle"))
{
//當碰撞發生的物體的tag為Obstacle則執行block裡的內
}
}
```
---
# Rigidbody
### 抓Rigidbody物件
遊戲物件可以透過套用不同的元件來增加需要的功能
``` csharp
private Rigidbody rigid_variable;
rigid_variable = GetComponent<Rigidbody>();//抓有物理特性的物件
```
```csharp
GetComponent<BoxCollider>();
//可以抓更細部的內容
GetComponent < BoxCollider > ().size.x / 2
//配合下圖觀看
```

### AddForce()
讓帶有rigidbody的物件移動,物體上給予一個方向的外力,使用AddForce時會受到物體質量影響。
```csharp=
rigid_variable.AddForce(vector3.up * 推力);
//向上跳
```
### ForceMode

```csharp=
rigid_variable.AddForce(vector3.up * 推力 ,ForceMode.Impulse);
//向上跳
```
---
# Animator Controller
### 從哪裡點進animation
雙擊兩下Animator Controller

### 預覽動作動畫
點選線以後可以在右下角預覽角色動畫

### 直接讓Entry可以跟動作連接

### 利用程式碼來給動畫
1. 宣告Animator變數
```csharp=
private Animator Animator_variable;
```
2. 抓Animator物件
```csharp=
Animator_variable = GetComponent<Animator>();
```
3. 給抓到的物件指定的動畫
#### 播放動畫
```csharp=
Animator_variable.SetTrigger("動畫名稱");
// **** 舉例 ****
Animator_variable.SetTrigger("Jump_trig");
//該物件得到跳的動畫
```
#### 給動畫名稱
下圖為一部份的動畫名稱:point_down:

#### SetBool、SetInteger
點選Alive->Death_01時

右方Inspector下方的condition有變成該動畫時的bool值跟DeathType為多少,如果想要使用該動畫,就在程式碼中寫SetBool、SetInteger來選擇要用哪個動畫。

**語法**
```csharp=
Animator_variable.SetBool("string_name", bool );
Animator_variable.SetInteger("string_name", int );
```
以Alive->Death_01為例
根據condition寫成 :point_down:
```csharp
playerAnim.SetBool("Death_b", true);
playerAnim.SetInteger("DeathType_int", 1);
```
___
# Animation
---
### particle
首先要先將動畫拉到要產生的物件變成子物件。

#### 變數宣告
```csharp=
public ParticleSystem variable;
```
記得把物件拉到裡面

#### 開始時自動播放
把play on Awake打勾 :heavy_check_mark:
#### 動畫播放
```csharp=
ParticleSystem_variable.Play();
```
#### 動畫停止
```csharp=
ParticleSystem_variable.Stop();
```
---
# Unity製作animation
windows->Animation->Animation->點選要製作成動畫的物件
根據想要做到的效果選取要改變的參數(poisition、rotation....)

將直立的白線放在想要的時間軸上,並對Scene裡的物件做出更動後按下

---
# 音效
在想加的物件裡,加入Audio Source
Add Component -> Audio Source

- Audio Clip
把音樂素材加到Audio Source裡的Audio Clip
- Loop
可以循環撥放音樂
- 在Audio Source裡必須把Play on Awake打開,利用Unpause()來播放,pause()暫停。
#### 宣告變數
``` csharp=
AudioClip 變數;
AudioSource 變數;
```
#### 抓AudioSource物件
```csharp=
AudioSource 變數 = GetComponent<AudioSource>();
```
#### PlayOneShot
plays an AudioClip,and scales the AudioSource volume by volumeScale。

**語法:**
```csharp=
AudioSource_variable.PlayOneShot(AudioClip 變數, 1.0f);
```
# 父子物件
子物件會以父為原點,position為子物件到父物件的距離。
ex:旋轉。子物件會以父為中心旋轉
# 迭代器實現延遲幾秒
### 語法
詳細資料請參考
[Coroutine](https://www.itread01.com/content/1550619739.html)
[IEnumerable、IEnumerator 與 IEnumerable](http://vegeee-csharp.blogspot.com/2017/02/c-ienumerableienumerator-ienumerable.html)
**呼叫迭代**
``` csharp=
StartCoroutine(迭代name());
```
**迭代本體**
``` csharp=
IEnumerator 迭代name() { //回傳值為yield return
//延遲前做的事情
yield return new WaitForSeconds(秒數);
//延遲後做的事情
}
```
---
# 轉場
1. File > Build Settings 先加入Scene
2. 程式碼
```csharp=
SceneManager.LoadScene(0); //or LoadScene("name");
```
- 重載目前場景
```csharp=
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
```
# 地形 Terrain
1. 建立一個地形(3D Object > Terrain)
2. 
(1)
(2) Paint 擦洗,就是雕塑地形

點開可以看到很多種,大多是用第一個建立出地形高低差

畫筆尺寸
不透明度
:::warning
滑鼠點擊是升高。
Shift按住是降低。
:::
如果覺得畫筆類型太少,可以去Asset store查Terrain Tools,有更新的補充包!
(3)
(4)
(5)Setting 整個地形大小在這裡設定