日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity 2D 打地鼠游戏制作过程总结

發布時間:2023/12/16 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);//當前位置生成mouse2Destroy(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、在同一位置已經出現地鼠,并且在還未擊打的情況下仍會重疊出現地鼠。(不足)

總結

以上是生活随笔為你收集整理的Unity 2D 打地鼠游戏制作过程总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。