Unity 2D遊戲製作
除錯背景
2D遊戲的製作最重要的是層的設定
先設定兩個層①Layers②Edit Layers③Sorting Layers新增兩個層BackGround和Front
除錯大雁
建立2D object改名BackGround設定背景圖片修改為BackGround 0層
建立2D object新增大雁圖片Ctrl+6,儲存,把大雁動態模型全選,插入。調整時間可以看起來舒服,調整所在層為BackGround 1層
新增指令碼使大雁從螢幕右邊開始飛入左邊飛出
publicclassSwanFly : MonoBehaviour {
//天鵝起飛的位置
privateVector3 startPosition;
publicfloat speed;
// Use this for initialization
void Start () {
Debug.Log("螢幕的寬度" + Screen.width);
Debug.Log("螢幕的高度" + Screen.height);
Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
float swanExtentsX = transform.GetComponent<Renderer>().bounds.extents.x;
startPosition = newVector3(screenSize.x + transform.position.y, transform.position.z);
transform.position = startPosition;
}
// Update is called once per frame
void Update () {
if (transform.position.x <-startPosition.x)
{
transform.position = startPosition;
}
transform.Translate(Vector3
}
}
調整草地
設定空物體,在其中建立草地,兩邊超過相機邊框。
在空物體上掛載BoxCollider2D元件,點選EditCollider 會有一個綠色方框調整位置
如圖所示即可
遊戲物件設定
設定球新增Front0層,掛載Circle Collider2D和Rigibody2D元件
建立一個2D空物體掛載指令碼,隨機產生球
publicclassCreatBall : MonoBehaviour {
//保齡球預製體
publicGameObject ballPrefab;
//產生的範圍
privatefloat createRange;
//產生的時間
publicfloat rate;
// Use this for initialization
void Start () {
Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
float ballExtentsX = ballPrefab.transform.GetComponent<Renderer>().bounds.extents.x;
createRange = screenSize.x -ballExtentsX;
}
// Update is called once per frame
void Update () {
rate -= Time.deltaTime;
if (rate<0)
{
Vector3 position = newVector3(Random.Range(-createRange, createRange),transform.position.y, transform.position.z);
GameObject ball = Instantiate(ballPrefab, position, Quaternion.identity) asGameObject;
//下一個保齡球產生的時間
rate = Random.Range(1.5f, 2.5f);
Destroy(ball, 3f);
}
}
}
除錯接物體的物體
建立兩個2D object分別新增帽子的內沿和外沿,外沿為內沿的子物體
內沿層Front 0層 外延層Front 1層
分別掛載Edge Collider 2D元件,內沿勾選Is Trigger,發生碰撞
設定Edit Collider
外沿 內沿
掛載指令碼接球然後銷燬產生粒子效果
publicclassHatController : MonoBehaviour {
//帽子移動的範圍
privatefloat HatMoveRange;
//粒子產生的位置
publicGameObject particleSystemPoint;
publicGameObject particle;
// Use this for initialization
void Start () {
Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
float hatExtentsX = transform.GetComponent<Renderer>().bounds.extents.x;
HatMoveRange = screenSize.x -hatExtentsX;
}
// Update is called once per frame
void Update () {
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float x = Mathf.Clamp(mousePosition.x,-HatMoveRange, HatMoveRange);
transform.position = newVector3(x, transform.position.y, transform.position.z);
}
voidOnTriggerEnter2D(Collider2D col)
{
//粒子產生
GameObject tempParticle = Instantiate(particle,particleSystemPoint.transform.position, Quaternion.identity)asGameObject;
Destroy(col.gameObject);
Destroy(tempParticle, 2f);
}
}
設定粒子效果
設定預製體的Renderer中 Sorting Layer為Front 2層
離子產生的位置設定一個2D空物體成為帽子內沿的子物體,調整位置