C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > Unity 2048游戏

基于Unity实现3D版2048游戏的示例代码

作者:极客柒

这篇文章主要为大家详细介绍了如何利用Unity实现简易的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;

            //发射
            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实现3D版2048游戏的示例代码的详细内容,更多关于Unity 2048游戏的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文