すでに作成済のクラスのいくつかにおいて、ゲームの設定用の数値を外部で設定・変更できるようにします。
デバッグがやりやすくなり、レベルデザインをする際にも役立ちます。
※ 参考
外部から設定可能なものの、ゲーム中にはスクリプトから変更できないようにする場合には、プロパティを活用します。(今回は未使用)
行と列に最小値と最大値を設定するようにすれば、設定した範囲内でランダムな値を取得することもできます。
1.ゲーム開始時に生成されるBoxの行と列を外部で設定可能にする
BoxInit.cs
public class BoxInit : MonoBehaviour {
[Header("Boxの種類数")] <= 追加
public GameObject [] boxObjPrefab;
public GameObject boxesObj;
[Header("Boxの行と列の設定値")] <= ここから
public int row; // 行の値
public int column; // 列の値 <= ここまで追加
// Boxのセットを行う(Ballが動くよりも前にセットが完了するようにAwakeで処理する)
void Awake() {
GameObject masterObj = GameObject.Find("Master");
for(int x = 0; x < row; x++) { <= [x < 8]の部分を変数に変更する
for(int y = 0; y < column; y++) { <= [y < 5]の部分を変数に変更する
int randomValue = Random.Range(0, boxObjPrefab.Length);
GameObject g = Instantiate(boxObjPrefab[randomValue], boxesObj.transform);
g.transform.position = new Vector3((2f + (1f * y)), 0.4f, (-4.2f + (1.2f * x)));
g.GetComponent<Destroyer>().masterObj = masterObj;
}
}
}
}
2.プレイヤーであるバーの移動速度を外部で設定可能にする
Controller.cs
public class Controller : MonoBehaviour {
public float playerSpeed; // Player(バー)の移動速度 <= 追加
void Update () {
if(Input.GetKey(KeyCode.LeftArrow)) {
transform.position += transform.forward * playerSpeed; <= [0.1f]を変数に変更
} else if (Input.GetKey(KeyCode.RightArrow)) {
transform.position -= transform.forward * playerSpeed; <= [0.1f]を変数に変更
}
}
}