生活随笔
收集整理的這篇文章主要介紹了
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
);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、在同一位置已經出現地鼠,并且在還未擊打的情況下仍會重疊出現地鼠。(不足)
總結
以上是生活随笔為你收集整理的Unity 2D 打地鼠游戏制作过程总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。