Unity的C#編程教程_51_method/function 方法應用練習題
Change Position Four Ways
- 在 unity 中改變遊戲對象的位置,我們需要使用
tranform.position
,設定一個new Vector3
比如我們把腳本掛載在一個 cube 下面:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(0, 0, 0); // 設置到原點位置
}
// Update is called once per frame
void Update()
{
}
}
遊戲啟動的時候,該 cube 就會被重置到原點的位置。
另一種方法,編寫一個方法:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
ChangePosition(); // 調用方法
}
// Update is called once per frame
void Update()
{
}
private void ChangePosition()
{
transform.position = Vector3.zero; // 重置位置到原點
}
}
可以起到同樣的效果
另外為了增加程式的復用性,我們可以把目標位置改成可傳入的參數:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
ChangePosition(new Vector3(0,0,0)); // 調用方法,並傳入位置參數
}
// Update is called once per frame
void Update()
{
}
private void ChangePosition(Vector3 targetPosition) // 設定一個傳入參數
{
transform.position = targetPosition; // 設定為目標位置
}
}
甚至,我們可以把獲取位置的部分放入一個新的方法中:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
ChangePosition(getPosition());
// 調用位置獲取方法,獲得位置
// 調用設定位置的方法,並位置
}
// Update is called once per frame
void Update()
{
}
private void ChangePosition(Vector3 targetPosition)
{
transform.position = targetPosition;
}
private Vector3 getPosition() // 獲取位置的方法
{
return Vector3.zero; // 返回原點的位置
}
}
為了更具靈活性,我們可以將獲取位置的方法改為需要傳入參數:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
ChangePosition(getPosition(1.0f, 2.0f, 3.0f));
}
// Update is called once per frame
void Update()
{
}
private void ChangePosition(Vector3 targetPosition)
{
transform.position = targetPosition;
}
private Vector3 getPosition(float x,float y,float z)
{
Vector3 targetPositon = new Vector3(x, y, z);
return targetPositon;
}
}
Challenge: Are You Alive?
- 任務說明:
- 設計程式,每次按下空格鍵,玩家扣血隨機值
- 然後返回玩家狀態:存活/死亡
- 玩家死亡後,按下空格鍵也不會調取扣血方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test4 : MonoBehaviour
{
public int health; // 血量
public int lowDamage; // 攻擊最小扣血
public int highDamage; // 攻擊最大扣血
public string result;
// Start is called before the first frame update
void Start()
{
health = 100; // 初始化血量
lowDamage = 1; // 初始化最小攻擊
highDamage = 10; // 初始化最大攻擊
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) // 按下空格表示一次攻擊
{
health = GetHit(health, lowDamage, highDamage); // 調用扣血方法
result = TestAlive(); // 調用方法檢測生存狀態
Debug.Log("Your health: " + health); // 顯示血量
Debug.Log(result); // 顯示狀態
}
}
private string TestAlive()
{
string result;
if (health > 0)
{
result = "You're still alive!";
}
else
{
result = "You're dead now!";
}
return result;
}
private int GetHit(int healthLeft,int low,int high)
{
healthLeft -= Random.Range(low, high); // 隨機扣血
if (healthLeft < 0)
{
healthLeft = 0;
}
return healthLeft;
}
}
這裡要注意,實現方法並不是唯一的!
另外,通常我們也可以設定一個 bool 變數用於存儲角色狀態(生存/死亡)
Return Array
- 任務說明:
- 創建 3 個 cube
- 把 Tag 都設置為 player
- 通過腳本選定這 3 個 cube
- 隨機改變 cube 的顏色
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test5 : MonoBehaviour
{
public GameObject[] allPlayers; // 初始化一個遊戲對象的 array
// Start is called before the first frame update
void Start()
{
allPlayers = FindAllPlayers(); // 找到所有的 Player,放入 array 中
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeColor(allPlayers); // 每次按下空格鍵調用改變顏色的方法
}
}
private GameObject[] FindAllPlayers()
{
GameObject[] allPlayers = GameObject.FindGameObjectsWithTag("Player");
return allPlayers;
}
private void ChangeColor(GameObject[] targets)
{
foreach(var item in targets) // 遍歷列表中的元素
{
item.GetComponent<MeshRenderer>().material.color = new Color(Random.value, Random.value, Random.value);
// 隨機改變顏色,其中 Random.value 獲得的值是 0~1,Color 的 3 個參數對應 RGB 通道
}
}
}
Challenge: Position Matters
- 任務說明:
- 用 array 保存 4 個不同的位置
- 使用一個方法生成隨機序號
- 使用另一個方法,根據序號更新遊戲對象的位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test6 : MonoBehaviour
{
public Vector3[] positions = new Vector3[4]; // 創建一個列表存放 4 個位置
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 4; i++) // 隨機生成 4 個位置
{
positions[i] = new Vector3(Random.value * 10, Random.value * 10, Random.value * 10);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ChangePosition();
}
}
private void ChangePosition() // 改變位置
{
transform.position = positions[RandomID()];
}
private int RandomID() // 生成隨機序號
{
return Random.Range(0, positions.Length);
}
}