unity 在2D区域 随机生成2D物体 并且不重叠

一 2d区域为父物体(涉及到UI变动自适应等)

 2d区域和2d物体 Itme的锚点设置  看自己  ,设置不同 算法 取值不同

public   Transform PosList;
        public GameObject Item;// 
void Start()
        {
         StartCoroutine( CreateTest());
        }


 List<float> vectorY = new List<float>();
        List<float> vectorX = new List<float>();
        List<Vector2> vector2s = new List<Vector2>();
        Vector2 pos = new Vector2();
        float _mapWidth;
        float _mapHight;
        public  IEnumerator CreateTest()
        {
            _mapWidth = PosList.GetComponent<RectTransform>().rect.width;//获取ui的实际宽度
            _mapHight = PosList.GetComponent<RectTransform>().rect.height;//长度
            Vector2 pos2D = PosList.position;
            Debug.Log("_mapWidth=" + _mapWidth + "_mapHight=" + _mapHight + "  pos2Dx=" + pos2D.x + "  pos2Dy=" + pos2D.y);
            Debug.Log("Item.transform.GetComponent<RectTransform>().rect.width=" + Item.transform.GetComponent<RectTransform>().rect.width );
            float ItmeWidth = Item.transform.GetComponent<RectTransform>().rect.width/2;
            float ItmeHeigh = Item.transform.GetComponent<RectTransform>().rect.height/2;
            for (int i = 0; i <100; i++)
            {
                do
                {
                    Debug.Log("执行do-----"+ "------i="+i);
                    Debug.Log("pos2D.x+ ItmeWidth=" + (pos2D.x + ItmeWidth)+ " _mapWidth + pos2D.x - ItmeWidth="+ (_mapWidth + pos2D.x - ItmeWidth));
                    Debug.Log("pos2D.y+ ItmeHeigh=" + (pos2D.y + ItmeHeigh) + " _mapHight + pos2D.y - ItmeHeigh=" + (_mapHight + pos2D.y - ItmeHeigh));
                    //随机 最小位置 左下角 (2d区域 x或者y + Itme的宽或者高) :保证 最小位置不出2d区域 左下角
                    //随机 最打位置 右上角 (2d区域 x或者y + 2d区域的宽或者高+Itme的宽或者高) :保证 最大位置不出2d区域 右上角
//DataTime.Now.Ticks 的值表示自 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔//的间隔数,可用于较精确的计时
                     Random.InitState((int)System.DateTime.Now.Ticks);
                    pos.x = Random.Range(pos2D.x+ ItmeWidth, _mapWidth + pos2D.x - ItmeWidth);
                    pos.y = Random.Range(pos2D.y+ ItmeHeigh, _mapHight + pos2D.y - ItmeHeigh);
                    Debug.Log("pos  y=" + pos.y + "  pos  x=" + pos.x);
                    //Debug.Log("Random.seed=" + Random.seed);
                    yield return null;
                } while (test02(pos));//(!vectorY.Contains(pos.y) && !vectorX.Contains(pos.x));   || BubbleSortY(vectorX, pos.x)

                Debug.Log("pos  y="+ pos.y+ "  pos  x="+ pos.x);
                if (pos.y<0){ Debug.LogError("pos.y<0  "+ pos.y);}
                if (pos.x < 0) { Debug.LogError("pos.x<0  " + pos.x); }

                vectorY.Add(pos.y);
                vectorX.Add(pos.x);
                vector2s.Add(pos);//不重复不重叠的 添加进List
                CreateItemPrefab(Item, UILevels.Low, pos);
            }
            
        }
       
        private bool test02(Vector2 p1)
        {
            for (int i = 0; i < vector2s.Count; i++)
            {
                if (TwoPointDistance2D(p1, vector2s[i])<145)//两个 中心点  必须大于这个数  //才不重叠 Itme 正方形 宽100,(圆形最适合这个计算)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 2维中如何计算两点之间的距离
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private float TwoPointDistance2D(Vector2 p1, Vector2 p2)
        {

            float i = Mathf.Sqrt((p1.x - p2.x) * (p1.x - p2.x)
                                + (p1.y - p2.y) * (p1.y - p2.y));

            return i;
        }

最后一个问题 为解决:随机到一定个数后,伪随机进入循环了  ,一直重复相近值, 无法完成100次随机

Unity2D鱼群算法是一种模拟鱼群行为的算法,通过模拟鱼类的移动、互动及群体行为,达到模拟真实鱼群行为的效果。该算法可以通过以下几个方面来实现: 1. 鱼的移动规则:首先,每一条鱼需要有自己的位置和速度信息。其次,每条鱼都需要根据周围鱼类的位置和速度信息进行调整,以避免碰撞和重叠。例如,当鱼与其他鱼过于接近时,它会调整自己的方向和速度,以保持一定的距离。 2. 群体行为规则:在鱼群中,每只鱼都可以感知到周围一定范围内的其他鱼类。通过感知其他鱼的位置和速度信息,鱼可以根据一定的规则来调整自己的行为。例如,当某个鱼发现周围有一条鱼在前进方向上移动较快时,它可能会跟随这条鱼的方向,以达到一定的集体行为效果。 3. 模拟环境:在Unity2D中,我们可以创建一个模拟环境,包含了鱼类的初始位置、速度和其他相关参数。通过在这个模拟环境中不断地更新每条鱼的位置和速度,我们可以模拟出鱼群的行为。同时,我们还可以添加其他的环境因素,如障碍物、食物等,来影响鱼群的行为。 总之,Unity2D鱼群算法是通过模拟鱼类的移动、互动和群体行为来实现的。通过规定鱼的移动规则和群体行为规则,并在一个模拟环境中进行实时更新,我们可以模拟出鱼群的行为,并实现更加真实的游戏体验。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野区捕龙为宠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值