基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼
分享三個(gè)無聊的時(shí)候用Unity寫的小游戲
包含 2048 2D版本和3D版本 Voodoo的小游戲 Sticky block

開源倉庫:
https://gitee.com/welcome2jcSpace/unity-30minute-mini-game
部分代碼展示
public class Cube : MonoBehaviour
{
public int index = 1;
public CubeLaunch mgr = null;
private Lively livelyScript = null;
private bool launched = false;
private Rigidbody rig = null;
private void Start()
{
livelyScript = GetComponent<Lively>();
rig = GetComponent<Rigidbody>();
}
//拖拽
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
if (launched) return;
if (null != livelyScript)
livelyScript.stop = true;
//得到cube 相對屏幕的位置
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
//得到相對偏移
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
if (launched) return;
//獲取當(dāng)前屏幕坐標(biāo)
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
//僅移動(dòng)x軸
var src = transform.position;
src.x = (Camera.main.ScreenToWorldPoint(curScreenPoint) + offset).x;
transform.position = src;
}
void OnMouseUp()
{
if (launched) return;
launched = true;
//發(fā)射
DoLaunch();
}
void DoLaunch()
{
// rig.velocity = Vector3.forward * 10;
mgr.aim.target = null;
}
private void Update()
{
if (launched)
{
transform.Translate(Vector3.forward * 20 * Time.deltaTime);
}
}
public void Upgrade()
{
if (null == mgr) return;
++index;
CubeLaunch.maxIndex = Mathf.Max(CubeLaunch.maxIndex, index);
this.tag = "preCube";
//設(shè)置紋理
var render = GetComponent<Renderer>();
render.material.mainTexture = mgr.textures[index];
//彈起
rig.AddForce(Vector3.up * 6.18f
+ Vector3.right * Random.Range(-2, 2)
+ Vector3.forward * Random.Range(-0.618f, 2)
, ForceMode.Impulse);
}
private void OnCollisionEnter(Collision collision)
{
var tag = collision.gameObject.tag;
if ("fixedCube" == tag || "end" == tag)
{
this.enabled = false;
this.tag = "fixedCube";
//var cube = collision.gameObject.GetComponent<Cube>();
//撞擊到cube
Cube cube = null;
collision.gameObject.TryGetComponent<Cube>(out cube);
if (null != cube && cube.index == this.index)
{
Destroy(this.gameObject);
cube.Upgrade();
}
mgr.Spawn();
}
}
}
以上就是基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Unity 2048游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#使用分部類設(shè)計(jì)實(shí)現(xiàn)一個(gè)計(jì)算器
分部類是C#4.5中的一個(gè)新特性,它的出現(xiàn)使得程序的結(jié)構(gòu)更加合理,代碼組織更加緊密,本文將使用分部類設(shè)計(jì)實(shí)現(xiàn)一個(gè)簡單的計(jì)算器,感興趣的小伙伴可以了解下2024-02-02
c#中WebService的介紹及調(diào)用方式小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中的WebService及其調(diào)用方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
C#使用ZXing實(shí)現(xiàn)二維碼和條形碼的生成
這篇文章主要為大家詳細(xì)介紹了C#如何使用ZXing實(shí)現(xiàn)二維碼和條形碼的生成與識別,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
ASP.NET總結(jié)C#中7種獲取當(dāng)前路徑的方法
本文主要介紹了7種獲取當(dāng)前路徑的方法,并做了代碼演示,分享給大家,感興趣的朋友可以參考一下。2016-03-03
SuperSocket入門--Telnet服務(wù)器和客戶端請求處理
本文的控制臺(tái)項(xiàng)目是根據(jù)SuperSocket官方Telnet示例代碼進(jìn)行調(diào)試的,官方示例代碼:Telnet示例。下面跟著小編一起來看下吧2017-01-01
Linq兩個(gè)List集合取交集的實(shí)現(xiàn)
這篇文章主要介紹了Linq兩個(gè)List集合取交集的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

