這次要完成的部分是1.隨時間增加樓梯移動速度 2.賦予不同種類的樓梯功能,完成後的結果可以點這裡。
首先要做改變樓梯速度之前,要先針對第一篇提到的腳本(GameObjectClone.cs)做修改,在做產生樓梯時是使用計時器去控制,但若遊戲加入加速功能後,會造成樓梯間距不同,所以要以距離為產生樓梯的基準,首先新增作為觸發器的Sprite物件,調整至適合的位置後增加Box Collider 2D及判斷觸發用的腳本(FloorCreate.cs),另外Box Collider 2D要將Is Trigger打勾才有辦法作為觸發器。
接著是腳本(FloorCreate.cs)的內容:
using UnityEngine;
using System.Collections;
public class FloorCreate : MonoBehaviour {
static public bool floorCreate = true;
void OnTriggerEnter2D(Collider2D collider){
if (collider.tag == "floor")
floorCreate = true;
}
}
在腳本內新增名為floorCreate的靜態布林變數,當OnTriggerEnter2D觸發時,若觸發對象tag為floor時,floorCreate設為true, 變數floorCreate的值就是是否要產生樓梯的開關,再來回到腳本(GameObjectClone.cs)做修改:using UnityEngine;
using System.Collections;
public class GameObjectClone : MonoBehaviour {
public GameObject [] floors = new GameObject[3];
private GameObject Clone;
void Update () {
if (FloorCreate.floorCreate)
{
int i = Random.Range(0, 3);
float j = Random.Range(-1.4f, 1.4f);
float posx = j;
Vector3 pos = new Vector3(posx, -2.0f, -5);
Quaternion rot = new Quaternion(0, 0, 0, 0);
Clone = (GameObject)Instantiate(floors[i], pos, rot);
Clone.AddComponent("FloorMove");
FloorCreate.floorCreate = false;
}
}
}
主要的改變為是否產生樓梯的if條件由時間修改成floorCreate的true or false,產生完樓梯後將floorCreate設為false,而腳本的其他部份就沒有變動。處理完樓梯間距後,回到樓梯加速的問題,需要在腳本(FloorMove.cs)上做處理:
using UnityEngine;
using System.Collections;
public class FloorMove : MonoBehaviour {
static public Vector3 speed;
static public float speedup = 0;
void Update () {
speedup += Time.deltaTime / 5000;
speed = new Vector3 (0, 0.03f+speedup, 0);
transform.position += speed;
}
}
這裡就使用簡單一點的方法,透過計時器累積來做加速,另外變數speed和speedup需要宣告為為static,在腳本(RestartController.cs)按下R鍵時設定回初始值:
public class RestartController : MonoBehaviour {
static public int lifepoint = 3;
.
.
.
void Update () {
.
.
.
if (lifepoint <= 0)
{
GameOverText.text = "遊戲結束 按R重新開始";
if (Input.GetKey (KeyCode.R))
{
.
.
.
FloorMove.speed = new Vector3(0, 0.03f, 0);
FloorMove.speedup = 0;
}
}
}
}
下一步就是各種類樓梯的功能,這裡在腳本(FloorMove.cs)加上以下內容:
using UnityEngine;
using System.Collections;
public class FloorMove : MonoBehaviour {
.
.
.
private float floorTimer = 0;
void OnCollisionStay2D(Collision2D collision){
if (collision.gameObject.name == "player_0" && gameObject.name == "floor2(Clone)")
collision.gameObject.transform.Translate (0.03f, 0, 0);
else if (collision.gameObject.name == "player_0" && gameObject.name == "floor3(Clone)")
collision.gameObject.transform.Translate (0, 0.5f, 0);
else if (collision.gameObject.name == "player_0" && gameObject.name == "floor5(Clone)") {
floorTimer += Time.deltaTime;
if(floorTimer>0.4f){
Destroy(gameObject);
floorTimer = 0;
}
}
}
void OnCollisionEnter2D(Collision2D collision){
if (collision.gameObject.name == "player_0" && gameObject.name == "floor4(Clone)") {
RestartController.lifepoint--;
}
}
.
.
.
}
先透過collision的name來判斷是否為玩家角色碰撞到樓梯,再透過gameObject.name來辨別樓梯種類,floor2至floor5分別對應到輸送帶、彈簧床、尖刺和翻轉樓梯,大致上就是做角色各方向的移動、扣血和樓梯消失等等功能,這裡就不多做詳細說明。下樓梯遊戲大致上就算完成了,至於一些小細節,比如說踩到樓梯要回血、移動速度怎樣才合理和樓梯種類生成的比例等等的問題,就不在多做討論。

我想問一下 為什麼我一測試我的GameObjectClone就會說 Missing
回覆刪除我想問一下 為什麼我一測試我的GameObjectClone就會說 Missing
回覆刪除