Space Shooter (作業五)

作業需求

遊戲素材下載

點我下載

建立遊戲

建立資料夾

首先開啟一個 2D 專案,之後新建立四個資料夾,分別名為

  • Textures – 用以存放遊戲影像
  • Scripts – 用以存放 C# 腳本
  • Prefabs – 存放遊戲的所有物件
  • Scenes – 存放遊戲的所有場景

進入 Textures 資料夾,將文章開頭下載的素材放入(共六張)

建立主要場景

按下「檔案」「另存為..」將目前場景存為主場景,名為「MainScene」

將圖片 background 以及太空船 spaceship 拉進去階層視窗

點擊 spaceship 物件修改屬性,按下「增加元件」「2D 物理」「Rigidbody」

並將「身體類型」變更為「Kinematic」

將 spaceship 物件位置設定為 -3.5

將 spaceship 的圖層順序設定為 1

主要物件完成

建立太空船控制腳本

新建一個 C# 腳本,名為「SpaceshipScript」

點擊進入編輯腳本,輸入以下程式碼

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

public class SpaceshipScript : MonoBehaviour
{
    // Start is called before the first frame update
    private Rigidbody2D r2d;
    void Start()
    {
        r2d = gameObject.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow))
            r2d.velocity = new Vector2(10, 0);
        else if (Input.GetKey(KeyCode.LeftArrow))
            r2d.velocity = new Vector2(-10, 0);
        else
            r2d.velocity = new Vector2(0, 0);
    }
}

將「SpaceshipScript」腳本拉到 spaceship 物件底下

透過左右方向鍵可以控制太空船左右移動

加入可以射擊的子彈

將子彈圖片加入場景中

修改子彈物件的屬性,按下「增加元件」「2D 物理」「Rigidbody」

並且將「身體類型」變更為「Kinematic」

幫子彈加入程式,新增 C# 腳本名為「BulletScript」

編輯 BulletScript 腳本,並且加入以下程式碼

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

public class BulletScript : MonoBehaviour
{
    // Start is called before the first frame update
    public int speed = 6;
    private Rigidbody2D r2d;
    void Start()
    {
        r2d = gameObject.GetComponent<Rigidbody2D>();
        r2d.velocity = new Vector2(0, speed);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
 
    void OnBecameInvisible()
    {
        Destroy(gameObject);
    }
}

將 BulletScript 腳本拉到 bullet 物件底下

修改 bullet 物件的圖層順序為1

將 bullet 做成預置物件

之後刪除在階層視窗的 bullet 物件

將 spaceship 的腳本「SpaceshipScript」修改為以下

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

public class SpaceshipScript : MonoBehaviour
{
    // Start is called before the first frame update
    private Rigidbody2D r2d;
    public GameObject bullet;
    void Start()
    {
        r2d = gameObject.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow))
            r2d.velocity = new Vector2(10, 0);
        else if (Input.GetKey(KeyCode.LeftArrow))
            r2d.velocity = new Vector2(-10, 0);
        else
            r2d.velocity = new Vector2(0, 0);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(bullet, transform.position, Quaternion.identity);
        }
    }
}

將 spaceship 物件底下的腳本,將預置物件的 bullet 拉進去

再來要把敵機(石頭)放進來,將 emeny 加進階層視窗

修改 enemy 物件的屬性,按下「增加元件」「2D 物理」「Rigidbody」

並且將「身體類型」變更為「Kinematic」

將圖層順序設定為1

新建一個腳本,名為「EnemyScript」

編輯 EnemyScript 腳本,並且輸入以下程式碼

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

public class EnemyScript : MonoBehaviour
{
    public int speed = -5;
    private Rigidbody2D r2d;
    // Start is called before the first frame update
    void Start()
    {
        r2d = gameObject.GetComponent<Rigidbody2D>();
        r2d.velocity = new Vector2(0, speed);
        r2d.angularVelocity = Random.Range(-200, 200);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnBecameInvisible()
    {
        Destroy(gameObject);
    }
}

將腳本 EnemyScript 拉近 enemy 物件底下

將 enemy 做成預置物件

刪除階層視窗的 enemy 物件

製作孵化器(產生器)

將圖片 spawn 拉到階層視窗,並設定Y位置為5

建立 C# 腳本名為「SpawnScript」

編輯腳本並加入以下程式碼

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

public class SpawnScript : MonoBehaviour
{
    public GameObject enemy;
    public float spawnTime = 2;
    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("addEnemy", 0, spawnTime);
    }

    void addEnemy()
    {
        Renderer rd = gameObject.GetComponent<Renderer>();
        float x1 = transform.position.x - rd.bounds.size.x / 2;
        float x2 = transform.position.x + rd.bounds.size.x / 2;
        Vector2 spawnPoint = new Vector2(Random.Range(x1, x2), transform.position.y);
        Instantiate(enemy, spawnPoint, Quaternion.identity);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

將 SpawnScript 腳本拉到 spawn 物件底下

將 enemy 預置物件加入

碰撞偵測

將 spaceship 物件與 bullet、enemy 預置物件都使用「增加元件」「2D 盒狀碰撞器」將「是觸發器」打勾

更新 EnemyScript 腳本

註:腳本內的 bullet(Clone) 與 spaceship 請與預置物件同名

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

public class EnemyScript : MonoBehaviour
{
    public int speed = -5;
    private Rigidbody2D r2d;
    // Start is called before the first frame update
    void Start()
    {
        r2d = gameObject.GetComponent<Rigidbody2D>();
        r2d.velocity = new Vector2(0, speed);
        r2d.angularVelocity = Random.Range(-200, 200);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnBecameInvisible()
    {
        Destroy(gameObject);
    }

    void OnTriggerEnter2D(Collider2D obj)
    {
        if (obj.gameObject.name.Equals("bullet(Clone)"))
        {
            Destroy(gameObject);
            Destroy(obj.gameObject);
        }
        if (obj.gameObject.name.Equals("spaceship"))
        {
            Destroy(gameObject);
        }
    }
}

遊戲完成,玩玩看吧!

SHXJ
Latest posts by SHXJ (see all)

在〈Space Shooter (作業五)〉中有 3 則留言

發佈留言