[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.
}
相关推荐
XR-AI-JK12 小时前
Unity VR/MR开发-开发环境准备
unity·vr·mr
T.D.C17 小时前
【渲染】Unity-分析URP的延迟渲染-DeferredShading
unity·游戏引擎
XR-AI-JK2 天前
Unity VR/MR开发-VR/开发SDK选型对比分析
unity·vr·mr
心之所向,自强不息2 天前
【Unity Shader编程】之让画面动起来
unity·游戏引擎
不伤欣3 天前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
Magnum Lehar3 天前
vulkan游戏引擎test文件memory实现
游戏引擎
Magnum Lehar3 天前
vulkan游戏引擎test_manager实现
java·算法·游戏引擎
快乐觉主吖3 天前
Unity的日志管理类
android·unity·游戏引擎
WarPigs3 天前
Unity性能优化笔记
笔记·unity·游戏引擎
T.D.C4 天前
【业务框架】3C-相机-Cinemachine
unity