程序世界生成器生成重叠的地砖 - Unity3D C#

Procedural world generator spawns overlapping ground tiles - Unity3D C#

我在编码方面有点菜鸟,如果我遗漏了一些明显的东西,我深表歉意。

我正在致力于以程序方式生成有限边界的垃圾场。我使用 2D unity 教程(在此处找到的 3 部分中的第 1 部分:https://www.youtube.com/watch?v=qAf9axsyijY&ab_channel=Blackthornprod )为我的代码制作骨架,但我遇到了地砖生成重叠的问题。我已经尝试了一些在线修复,但都无法完全完成工作。

Physics.Overlap 使代码工作得更好,但仍然有规律的重叠。我怀疑重叠的图块可能在完全相同的实例中生成,这使得 Overlap 命令 return 0 对撞机错误,但我不知道如何测试或避免它。

这是我的代码。几乎是直接照搬上面的视频。:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawnTile : MonoBehaviour
{
    public int openingDirection;
    //1 = N
    //2 = E
    //3 = S
    //4 = W

    private GroundTemplates templates;
    //Stores tiles to spawn

    private int rand;
    private bool spawned = false;
    private bool IsInBounds = false;
    public LayerMask checkBox;
    //Layermask to check if there is already ground at spawn point
    public Vector3 chunkScale;
    //size of check area for dup spawns
    void Start()
    {
        templates = GameObject.FindGameObjectWithTag("GroundTiles").GetComponent<GroundTemplates>();
        StartCoroutine(Spawn());
    }
    public IEnumerator Spawn()
    {
        Collider[] hitColliders = Physics.OverlapBox(transform.position, chunkScale , Quaternion.identity, checkBox); // makes an array of objects inside chunk with layer mask checkBox. Used for checking if ground is present
        yield return new WaitForSeconds(.2f); 
        Debug.Log("colliders: " + hitColliders.Length);
        if (spawned == false )
        {
            if (openingDirection == 1 && IsInBounds == true && hitColliders.Length == 0)
            {
                rand = Random.Range(0, templates.NorthRooms.Length);
                Instantiate(templates.NorthRooms[rand], transform.position, templates.NorthRooms[rand].transform.rotation);
            }
            else if (openingDirection == 2 && IsInBounds == true && hitColliders.Length == 0)
            {
                rand = Random.Range(0, templates.EastRooms.Length);
                Instantiate(templates.EastRooms[rand], transform.position, templates.EastRooms[rand].transform.rotation);
            }
            else if (openingDirection == 3 && IsInBounds == true && hitColliders.Length == 0)
            {
                rand = Random.Range(0, templates.SouthRooms.Length);
                Instantiate(templates.SouthRooms[rand], transform.position, templates.SouthRooms[rand].transform.rotation);
            }
            else if (openingDirection == 4 && IsInBounds == true && hitColliders.Length == 0)
            {
                rand = Random.Range(0, templates.WestRooms.Length);
                Instantiate(templates.WestRooms[rand], transform.position, templates.WestRooms[rand].transform.rotation);
            }
            spawned = true;
           
        }
    }



    private void OnTriggerEnter(Collider other)
    {
        
             if (other.CompareTag("Spawn") && (other.GetComponent<spawnTile>().spawned == true))
        {
            Destroy(gameObject);
            Debug.Log("Destroyed");
        }
             else if (other.CompareTag("InBounds"))
        {
            IsInBounds = true;
        }
    }
}

一些输出图片:

a generated map

two overlapping tiles

spawn point prefab that code is attached to

ground tile prefab example

我假设多次调试尝试后的代码中存在一些冗余,如果我可以简化事情,请问一下。

快速浏览视频显示没有迹象表明原作者使用协程生成新图块。

对于您生成的每个图块,您检查是否有重叠,然后等待 0.2 秒。正是在那里等待可以很容易地让不同的图块也检查相同的 space 并发现没有图块重叠。所以两个或更多的瓦片将看不到重叠,并且都写入相同的位置。

我可以建议清理代码,删除强制延迟并尝试这样做:

void Start ( )
{
    templates = GameObject.FindGameObjectWithTag ( "GroundTiles" ).GetComponent<GroundTemplates> ( );

    var hitColliders = Physics.OverlapBox ( transform.position, chunkScale, Quaternion.identity, checkBox ); // makes an array of objects inside chunk with layer mask checkBox. Used for checking if ground is present
    Debug.Log ( "colliders: " + hitColliders.Length );
    if ( !spawned )
    {
        if ( IsInBounds && hitColliders.Length == 0 )
        {
            GameObject [ ] rooms = null;
            switch ( openingDirection )
            {
                case 1: rooms = templates.NorthRooms; break;
                case 2: rooms = templates.EastRooms; break;
                case 3: rooms = templates.SouthRooms; break;
                case 4: rooms = templates.WestRooms; break;
                default: return;
            }

            var room = rooms [ UnityEngine.Random.Range ( 0, rooms.Length ) ];
            Instantiate ( room, transform.position, room.transform.rotation );
        }
        spawned = true;
    }
}

在 Milan 的帮助下,我在此线程中获得了代码。使用他的代码后。我使用序列化字段查看未满足哪些条件,发现 InBounds 检查未通过。所以我修改了检查,使其发生在 Start() 而不是 OnTriggerEnter();

我刚想到这个解决方案,如果您有有用的反馈,请告诉我。

这是新代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class betterTileSpawner : MonoBehaviour
{
    public int openingDirection;
    //1 = N
    //2 = E
    //3 = S
    //4 = W

    public LayerMask checkBox;
    //Layermask to check if there is already ground at spawn point
    public Vector3 chunkScale;
    //size of check area for dup spawns
    private Collider Boundarys;
    //Used for 
    private GroundTemplates templates;
    //Stores tiles to spawn

    private int rand;
    [SerializeField]
    private bool spawned = false;
    [SerializeField]
    private bool IsInBounds = false;
    [SerializeField]
    private string Helper;
    [SerializeField]
    private int NumColliders;

    IEnumerator Start()
    {
        templates = GameObject.FindGameObjectWithTag("GroundTiles").GetComponent<GroundTemplates>();
        yield return new WaitForSeconds(.01f);
        var hitColliders = Physics.OverlapBox(transform.position, chunkScale, Quaternion.identity, checkBox); // makes an array of objects inside chunk with layer mask checkBox. Used for checking if ground is present
        Boundarys = GameObject.FindGameObjectWithTag("InBounds").GetComponent<Collider>();
        Debug.Log("colliders: " + hitColliders.Length);
        NumColliders = hitColliders.Length;
        Helper = "didn't do any ifs";


        if (Boundarys.bounds.Contains(transform.position))
            { IsInBounds = true; }

        if (!spawned)
            {  

            Helper = "Not Spawned " + NumColliders;
            Debug.Log("Not Spawned");
                if (IsInBounds && hitColliders.Length == 0)
                {
                    GameObject[] rooms = null;
                        switch (openingDirection)
                        {
                            case 1: rooms = templates.NorthRooms; break;
                            case 2: rooms = templates.EastRooms; break;
                            case 3: rooms = templates.SouthRooms; break;
                            case 4: rooms = templates.WestRooms; break;
                            default: Debug.Log("Default case"); break;
                        }

                    var room = rooms[UnityEngine.Random.Range(0, rooms.Length)];
                    Instantiate(room, transform.position, room.transform.rotation);

                    Helper = "Should have spawned, wenth through code";
                    Debug.Log("Spawned");
                    spawned = true;
                }
            
            }
    }


    private void OnTriggerEnter(Collider other)
    {

        if (other.CompareTag("Spawn") && (other.GetComponent<betterTileSpawner>().spawned == true))
        {
            Destroy(gameObject);
            Debug.Log("Destroyed");
        }
        else if (other.CompareTag("Ground"))
        {
            Destroy(gameObject);
            Debug.Log("Destroyed");
        }
    }
}