服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 基于Unity实现3D版2048游戏的示例代码

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

2023-03-09 14:02极客柒 C#

这篇文章主要为大家详细介绍了如何利用Unity实现简易的3D版2048游戏,文中的示例代码讲解详细,具有一定的学习价值,需要的可以参考一下

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

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

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

开源仓库:

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游戏的资料请关注服务器之家其它相关文章!

原文地址:https://blog.csdn.net/qq_39162566/article/details/128836309

延伸 · 阅读

精彩推荐
  • C#C#代码实现PDF文档操作类

    C#代码实现PDF文档操作类

    本篇文章给大家介绍使用pdf文档操作C#代码,本文代码非常简单,代码附有注释,需要注意的是:需要添加itextsharp.dll引用才可以正常通过编译,感兴趣的朋...

    C#教程网3992021-11-01
  • C#C# 是 TypeScript 的最佳替补?

    C# 是 TypeScript 的最佳替补?

    TypeScript非常优秀。它完美地结合了强类型和快速开发,因此非常好用,我在许多情况下都会默认选择这个库。但是,世上没有完美的语言,有些情况下Ty...

    晃來晃呿11002021-12-29
  • C#C#中TCP粘包问题的解决方法

    C#中TCP粘包问题的解决方法

    这篇文章主要为大家详细介绍了C#中TCP粘包问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    白云随风5812022-01-17
  • C#C#中数组Array,ArrayList,泛型List详细对比

    C#中数组Array,ArrayList,泛型List详细对比

    关于数组Array,ArrayList,泛型List,简单的说数组就是值对象,它存储数据元素类型的值的一系列位置.Arraylist和list可以提供添加,删除,等操作的数据. 具体如何...

    C#教程网12052021-11-26
  • C#C# 扩展方法的使用

    C# 扩展方法的使用

    这篇文章主要介绍了C# 扩展方法的使用,帮助大家更好的理解和学习c#编程语言,感兴趣的朋友可以了解下...

    一线码农5352022-10-25
  • C#unity学习教程之定制脚本模板示例代码

    unity学习教程之定制脚本模板示例代码

    这篇文章主要给大家介绍了关于unity学习教程之定制脚本模板的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    禹泽鹏鹏9972022-03-07
  • C#C#简单实现在网页上发邮件的案例

    C#简单实现在网页上发邮件的案例

    本文分享一个C#利用SMTP发送邮件的案例,提供了前后台代码,方便大家学习。...

    攻城狮caitou9032021-11-16
  • C#C# 设计模式系列教程-抽象工厂模式

    C# 设计模式系列教程-抽象工厂模式

    抽象工厂模式为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类。...

    Wang Juqiang10912021-11-23