基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼
更新時(shí)間:2023年02月02日 08:33:15 作者:極客柒
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)簡易的3D版2048游戲,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
分享三個(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)文章
unity實(shí)現(xiàn)多點(diǎn)觸控代碼
這篇文章主要介紹了unity實(shí)現(xiàn)多點(diǎn)觸控代碼,我最近在學(xué)習(xí)Unity游戲引擎。先從Unity平面開始,本章介紹Unity 平面上的多點(diǎn)觸摸。有需要的小伙伴參考下。2015-03-03
WinForm使用DecExpress控件中的ChartControl插件繪制圖表
這篇文章介紹了WinForm使用DecExpress控件中的ChartControl插件繪制圖表的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法
這篇文章主要介紹了C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法,涉及C#操作系統(tǒng)進(jìn)程的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

