开发者

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

分享三个无聊的时候用Unity写的小游戏

包含 2048 2D版本和3D版本 Voodoo的小游戏 Sticky block

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

开源仓库:

https://gitee.com/welcome2jcSpace/unity-30minute-mini-javascriptgame

部分代码展示

  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 = GetCompone开发者_开发学习nt<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);

lGNuZ            //得到相对偏移
            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 Vectorwww.devze.com3(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;
            CulGNuZbeLaunch.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)
        {

   http://www.devze.com         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游戏的资料请关注我们其它相关文章!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜