关于简单的打地鼠游戏开发总结
1.
鼠標點擊函數制作:
首先我們需要建立好背景圖,調整攝像機為正交模式,進入二維狀態,調整背景圖分辨率為適當大小。
制作鼠標點擊腳本思路為先點擊然后銷毀對象以下為函數
為此函數銷毀被鼠標點擊的對象Destroy(gameObject);
然后制作一個預制體,是地鼠被打的圖片,在地鼠被鼠標點擊后存在0.5s左右的時間顯示地鼠被打的圖片,代碼整體為:using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
public class S3 : MonoBehaviour
{
// Start is called before the first frame
update
void Start()
{
Destroy(gameObject, 0.5f);
}
// Update is called once per frame
void
Update()
{
}
}
掛在被打地鼠狀態身上即可
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
public class S2 : MonoBehaviour
{
// Start is called before the first frame
update
public
GameObject m_Prefab2;
void Start()
{
}
void
OnMouseDown()
{
Instantiate(m_Prefab2, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
掛在地鼠圖片上即可(地鼠圖片上必須掛上碰撞體組件)2.
制作地鼠隨機在孔中出來函數和隨機生成地鼠
把兩張地鼠圖片都制作成預制體然后在地鼠預制體圖片身上掛上制作的隨機生成代碼如下:
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
public class CreateTarget : MonoBehaviour
{
public
GameObject m_Target;
void Start()
{
// Create();
InvokeRepeating(“Create”, 0, 0.5f);
}
void
Create()
{
Vector3 pos = Vector3.zero;
int id = 0;
id = Random.Range(1,13);
if(id==1)
pos = new
Vector3(-0.464f, 0.201f, 0);
if (id ==
2)
0.201f, 0);
if (id ==
3)
if (id ==
4)
if (id ==
5)
if (id ==
6)
if (id ==
7)
if (id ==
8)
if (id ==
9)
if (id ==
10)
if (id ==
11)
if (id ==
12)
Instantiate(m_Target, pos, Quaternion.identity);
//Instantiate(m_Target,)
}
}
由于我的圖片是十二個洞口,于是最簡單的方法做了十二個id,用Random.Range函數隨機生成。
3.最后給兩張地鼠的預制體掛上音效即可。
如圖:
找到合適的音效
掛在合適的對象上
總結
以上是生活随笔為你收集整理的关于简单的打地鼠游戏开发总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 短线王必备技巧
- 下一篇: 深入剖析线程同步工具CountDownL