基于Unity實現(xiàn)3D版2048游戲的示例代碼
更新時間:2023年02月02日 08:33:15 作者:極客柒
這篇文章主要為大家詳細介紹了如何利用Unity實現(xiàn)簡易的3D版2048游戲,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下
分享三個無聊的時候用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; //獲取當前屏幕坐標 Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); //僅移動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"; //設置紋理 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實現(xiàn)3D版2048游戲的示例代碼的詳細內(nèi)容,更多關于Unity 2048游戲的資料請關注腳本之家其它相關文章!
相關文章
c#中WebService的介紹及調(diào)用方式小結(jié)
這篇文章主要給大家介紹了關于c#中的WebService及其調(diào)用方式的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-11-11SuperSocket入門--Telnet服務器和客戶端請求處理
本文的控制臺項目是根據(jù)SuperSocket官方Telnet示例代碼進行調(diào)試的,官方示例代碼:Telnet示例。下面跟著小編一起來看下吧2017-01-01