<!-- css 字體設定 -->
<style>
.reveal {
font-size: 24px;
}
</style>
# 虛擬實境4-3
<!--放入主題照片 統一 =480x320 -->

[原始課程 link](https://learn.unity.com/tutorial/lesson-4-3-powerup-and-countdown?uv=2018.4&courseId=5cf96c41edbc2a2ca6e8810f&projectId=5cf96846edbc2a2bcde6d0fc)
###### tags: `虛擬實境AA`
----
## 課程目標
- 使用變數學習寫出除錯訊息(debug)
- 學習使用IEnumerator和Coroutines
- 利用SetActive讓物件消失或出現
----
## 新增一個BUFF物件
----

在`Project` `Assets` `Pickups` 中找到自己喜歡的物件
拖移至上方的`Hieirarchy`中
----

將名稱改名為`Powerup`
----

在`Inspector` `Transform` `Scale` 中
將XYZ都改成`2`
----
## 將物件實體化
----

在`Inspector`的最下方點選`Add Component`
搜尋並選取`Box Collider`
----

將剛剛新增的`Box Collider`
找到`is Trigger`並打勾
此時會發現`Powerup`的周圍會亮起深色的線 當物件經過碰撞後 就會消失
----

回到`Inspector`上方 找到`Tag`
點擊並選擇`Add Tag...`
----

點擊`+`並改名為`Powerup`按下`Save`
----

回到`Powerup` 找到`Tag`選取`Powerup`
----

將`Hierarchy`裡的`Powerup`拖移至下方的`Assets` `Prefabs`上
----

選取`Original Prefab`
----

找到並點開`Project` `Assets` `Scripts`的`PlayerController`
----

在`public class`中打上
```csharp
public bool hasPowerup = false;
private void OnTriggerEnter(Collider other){
if(other.CompareTag("Powerup")){
hasPowerup = true;
Destroy(other.gameObject);
}
}
```
----
## 設定Enemy的標籤
----

在`Project` 點擊`Prefabs`中的 `Enemy`
並在右上方找到`Tag`選取`Add Tag...`
----

點擊`+`號
新增一個名叫`Enemy`的Tag後
按下`Save`
----

回到`Project` `Prefabs` `Enemy`
同樣點擊Tag並選取剛才新增的`Enemy`Tag
----
## 設定玩家得到的附加效果
----

打開`PlayerController.cs`
在`void Update`中打上
```csharp
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Enemy") && hasPowerup)
{
Debug.Log("Player collided with" + collision.gameObject + " with powerup set to " + hasPowerup);
}
}
```
----

在`public class`中打上
```csharp
private float powerupStrength = 15.0f;
```
----
## 設定讓敵人不因為與玩家的距離越遠而增大敵人的速度
----

在`private void OnCollisionEnter`中加上
```csharp
Rigidbody enemyRigidbody = collision.gameObject.GetComponent<Rigidbody>();
Vector3 awayFromPlayer = (collision.gameObject.transform.position - transform.position);
enemyRigidbody.AddForce(awayFromPlayer * powerupStrength, ForceMode.Impulse);
```
----
## 設定附加效果的持續時間
----

在`private void OnTriggerEnter`中加上
```csharp
StartCoroutine(PowerupCountdownRoutine());
```
接著如圖在下方加上一個函式
```csharp
IEnumerator PowerupCountdownRoutine()
{
yield return new WaitForSeconds(7);
hasPowerup = false;
}
```
----
## 製作獲得附加效果的特效
----

在`Course Library` `Powerup indicators`選擇一個自己喜歡的特效,拉上去`Hierarchy`中
並命名為`Powerup Indicator`
----

調整`Powerup Indicator`的`scale`的XYZ至3
----

調整`Powerup Indicator`的`Position`的Y軸
----

將`Powerup Indicator`的旁邊的勾勾取消勾選
讓效果先不要出現在場景直到吃到效果
----
## 控制效果的程式碼
----

打開`PlayerController.cs`
在外面先命名一個
```csharp
public GameObject powerupIndicator;
//在void Update()加上 "效果位置"
powerupIndicator.transform.position = transform.position + new Vector3(0, -0.5f, 0);
//在private void OnTriggerEnter(Collider other){
//if(other.CompareTag("Powerup"))}中加上 "啟動效果"
powerupIndicator.gameObject.SetActive(true);
//在IEnumerator PowerupCountdownRoutine()中加上 "結束效果"
powerupIndicator.gameObject.SetActive(false);
```
----

回到`player`
將`Hierarchy`中的`Powerup Indicator`拖移到`Inspector`下方`Player Controller`的`Powerup Indicator`中
----
可以前往下一小節了
{"metaMigratedAt":"2023-06-15T15:07:09.157Z","metaMigratedFrom":"YAML","title":"虛擬實境4-3","breaks":true,"contributors":"[{\"id\":\"06326c0f-824e-4964-b604-fd9b52334bb2\",\"add\":4649,\"del\":266}]"}