Unity 2D 打地鼠遊戲製作過程總結

一、過程總結

(一)搭建場景

1、創建新項目——2D。
在這裏插入圖片描述
2、新建文件夾Sprites用於存放所需素材。
在這裏插入圖片描述
3、將ground拖入scene並根據圖片進行分辨率的設置。
在這裏插入圖片描述
4、調整攝影機size。
在這裏插入圖片描述
5、新建空物體Map,將ground和Hole作爲Map的子物體,並將Hole的Order in Layer 值設置爲1,ground的Order in Layer爲0,確保Hole在ground上層顯示。
在這裏插入圖片描述
在這裏插入圖片描述
6、保存場景爲s1。
在這裏插入圖片描述

(二)製作地鼠

1、添加地鼠素材Gophers和Gophers_Beaten,並添加Box Collider 2D。
在這裏插入圖片描述
2、在Gophers上添加腳本Click,Gophers_Beaten上添加腳本Disappear,用於控制單擊地鼠後的效果。
在這裏插入圖片描述

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

public class Click : MonoBehaviour {

    public GameObject mouse2;

	void Start () {
        Destroy(gameObject,1.5f);
    }

    void OnMouseDown() {
        Instantiate(mouse2,transform.position,Quaternion.identity);//當前位置生成mouse2
        Destroy(gameObject);//點擊銷燬
    }
	

}

在這裏插入圖片描述

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

public class Disappear : MonoBehaviour {

    void Start () {
        Destroy(gameObject,0.5f);
	}
	
	void Update () {
		
	}
}

3、將Gophers和Gophers_Beaten作爲預製體。
在這裏插入圖片描述
4、新建腳本CreateTarget用來控制地鼠的隨機生成。將腳本添加至空物體CreateTarget。
在這裏插入圖片描述

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

public class CreateTarget : MonoBehaviour {

    public GameObject mouse1;

	void Start () {
        InvokeRepeating("Create",0,1);
	}

    void Create() {
        Vector3 pos = Vector3.zero;

        int id = 0;
        id = Random.Range(0, 9);//產生隨機數

        switch (id) {
            case 0:
                pos = new Vector3(-2,1,0);
                break;
            case 1:
                pos = new Vector3(-2,0, 0);
                break;
            case 2:
                pos = new Vector3(-2,-1, 0);
                break;
            case 3:
                pos = new Vector3(0,1, 0);
                break;
            case 4:
                pos = new Vector3(0,0, 0);
                break;
            case 5:
                pos = new Vector3(0,-1, 0);
                break;
            case 6:
                pos = new Vector3(2,1, 0);
                break;
            case 7:
                pos = new Vector3(2,0, 0);
                break;
            case 8:
                pos = new Vector3(2, -1, 0);
                break;
        }
        Instantiate(mouse1,pos,Quaternion.identity);  

      
    }
	
	void Update () {
		
	}
}

5、添加聲音appear給預製體Gophers,beaten給預製體Gophers_Beaten。
在這裏插入圖片描述

(三)分數設置

1、添加UI——Text,設置Text位置、大小、顏色等信息。
在這裏插入圖片描述
2、新建Score腳本,對分數進行設置。
在這裏插入圖片描述

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

public class Score : MonoBehaviour {
    public static int score;
    Text text;
	void Start () {
        text = GetComponent<Text>();
        score = 0;
	}
	
	void Update () {
        text.text = "Score:" + score;
	}
}

3、在Disappear腳本中添加有關分數的語句。
在這裏插入圖片描述

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

public class Disappear : MonoBehaviour {
    int scoreValue = 10;
    void Start () {
        Destroy(gameObject,0.5f);
        Score.score += scoreValue;
	}
	
	void Update () {
		
	}
}

(四)完成

在這裏插入圖片描述

二、收穫總結

1、3D物體自帶Box Collider,而2D物體則需要自行添加Box Collider 2D方可使用;
2、利用 InvokeRepeating函數實現了地鼠出現位置的隨機生成;
3、在同一位置已經出現地鼠,並且在還未擊打的情況下仍會重疊出現地鼠。(不足)

發佈了1 篇原創文章 · 獲贊 2 · 訪問量 686
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章