[Unity]在场景中随机生成不同位置且不重叠的物体

1.前言

最近任务需要用到Unity在场景中随机生成物体,且这些物体不能重叠,简单记录一下。

参考资料:How to ensure that spawned targets do not overlap ?

2.结果与代码

结果如下所示:

代码如下所示:

c 复制代码
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts
{
    public class NewMonoBehaviour : MonoBehaviour
    {

 
        public GameObject objectForSpawn;
        private List<SpawnObject> spawnObjectList;
        private int objectCount = 50;

        private void Start()
        {
            CreateCollections();
            Generate(objectCount);
        }
        void CreateCollections()
        {
            spawnObjectList = new List<SpawnObject>();
        }
        void Generate(int objectCount)
        {
            CreateObjects();
            void CreateObjects()
            {
                for (int i = 0; i < objectCount; i++)
                {
                    Vector3 objectPosition = GetPosition();
                    SpawnObject sO = new SpawnObject(objectPosition);
                    spawnObjectList.Add(sO);
                }
            }     
            SpawnGameObjects();
            void SpawnGameObjects()
            {
            
                List<SpawnObject> spawnedObjects = new List<SpawnObject>();
                List<SpawnObject> notSpawnedObjects = new List<SpawnObject>();

                foreach (SpawnObject item in spawnObjectList)
                {
                    Vector3 spawnPosition = GetPosition();
                    if (item.objectPosition != spawnPosition)
                    {
                        Instantiate(objectForSpawn, spawnPosition, transform.rotation);
                        spawnedObjects.Add(item);
                    }
                    else
                    {
                        notSpawnedObjects.Add(item);
                    }
                }
                Debug.Log("Object spawned :" + spawnedObjects.Count);
                Debug.Log("Object notSpawned :" + notSpawnedObjects.Count);
            }
        }
        Vector3 GetPosition()
        {
            Vector3 randmPosition;
            randmPosition.x = Random.Range(1, 100);
            randmPosition.y = Random.Range(1, 100);
            randmPosition.z = Random.Range(1, 100);
            return randmPosition;
        }
    }
    public class SpawnObject
    {
        public Vector3 objectPosition;
        public SpawnObject(Vector3 objectPosition)
        {
            this.objectPosition = objectPosition;
        }

    }
//In case you want serialize this position for example for save you better use float[3] for position then vector.
}
相关推荐
电子云与长程纠缠10 小时前
UE5 Lyra PocketWorld进行3D内容UI预览 - 上
开发语言·学习·3d·ue5·游戏引擎
玖玥拾10 小时前
Unity3D RPG 入门项目(八)游戏设置面板、帧率控制、快捷技能药品栏、技能解锁系统
游戏·3d·unity·游戏引擎
ellis197011 小时前
u3d插件xLua[十] lua侧判空问题
unity
牛哇网络工作室21 小时前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
ellis19701 天前
u3d插件xLua[九]例8 Hotfix
unity
淡海水1 天前
01-07-运行时-GC深度剖析-内存分配回收与结构选择
jvm·windows·unity·gc
优梦创客1 天前
游戏开发架构选型:第1篇|什么是架构?从进球事件看 MVC 的局限
unity·架构·游戏引擎·游戏开发
zhangphil2 天前
Unity3D(Unity)跨平台游戏引擎简介
unity
Madokaly2 天前
DOTween动画:TweenManager深度解析
unity
_ZHOURUI_H_3 天前
Unity TCP底层极限拆解:粘包、半包、CRC、序列号、双缓冲到底怎么一起工作的?
网络·网络协议·tcp/ip·游戏·unity·游戏引擎·游戏开发