Unity 2D 射擊遊戲教呈 01

配合入門者快速入門,此教呈採取最基礎程序來製作。

  • 遊戲設計製作之前,要先調整解析度比例之問題,這裡直接以1024 * 768 解析度來當成 一個範例教學製作。
  • 2D射擊遊戲包含物件(教呈順序): 
    1. 玩家(水平移動及發射子彈)
    2. 敵人
    3. 子彈
    4. 爆炸特效
    5. 介面 (分數、菜單、玩家屬性)
-----------------------------------------------
Step1 :  開啟專案後請修改MainCamera 物件之Camera組件底下的Projection屬性調整為Orthographic。













Step2: 內建模型當成玩家代表,此這裡當成玩家代表模型為: Cube。













Step3: 先建立資料料夾,名稱為: Script,在Script 資料夾裡面建立C#腳本名稱為: Player

Step4: 玩家移動程式碼如下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    public float speed = 10f;  // 速度

    void Update()
    {
        // 取得Unity Editor => Project Setting => Input Message
        float Horizontal = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
        // 座標(X軸)移動
        transform.Translate(Vector3.right * Horizontal);
    }
}
Step5: 再把程式碼套用至Cube 物件上面,若是成功,就能水平移動。













Step6: 移動會發現到玩家會超出界線,所以要讓玩家左邊出右邊進的方法,因為目前我們玩家移動空間範圍只有Camera看的到的範圍,因此我們可以直接綁定界線座標。













Step7: 知道界線座標後,就可以撰寫程式,讓玩家開始左初右進或右出左進。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speed = 10f;  // 速度

    void Update()
    {
        // 取得Unity Editor => Project Setting => Input Message
        float Horizontal = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
        // 座標(X軸)移動
        transform.Translate(Vector3.right * Horizontal);

        // 右出左進
        if (transform.position.x >= 6.1f)
            transform.position = new Vector3(-6.1f, transform.position.y, transform.position.z);
        // 左出右進
        else if (transform.position.x <= -6.1f)
            transform.position = new Vector3(6.1f, transform.position.y, transform.position.z);
    }
}
Step8: 成功之後,會出現玩家左初右進或右出左進行為,之後把玩家位置調整到最底下,玩家大致上移動行為就完成了,剩下就是發射子彈了。












Step9: 建立一個子彈物件,建議物件為: Sphere,之後再Projec視窗建立資料夾,資料夾名稱為: Prefab,再把子彈物件拖曳進去,之後再把Hierachy裡面的子彈物件按下Delect刪除。












Step10: 之後撰寫生產子彈物件程式碼,確認成功後,玩家物件行為完成,程式碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speed = 10f;  // 速度
    public GameObject bullet;

    void Update()
    {
        // 取得Unity Editor => Project Setting => Input Message
        float Horizontal = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
        // 座標(X軸)移動
        transform.Translate(Vector3.right * Horizontal);

        // 右出左進
        if (transform.position.x >= 6.1f)
            transform.position = new Vector3(-6.1f, transform.position.y, transform.position.z);
        // 左出右進
        else if (transform.position.x <= -6.1f)
            transform.position = new Vector3(6.1f, transform.position.y, transform.position.z);

        // 取得Unity Editor => Project Setting => Input Message
        if (Input.GetButtonDown("Jump"))
            Instantiate(bullet, transform.position, Quaternion.identity);
    }
}
Step 11:之後把程式碼Bullet空物件,把Project子彈物件拖曳進去即可。

留言