问题描述
我在编码方面有点菜鸟,如果我遗漏了一些明显的东西,很抱歉。
我正致力于在程序上生成一个有限边界的垃圾场。我使用了 2D 统一教程(此处为 3 部分中的第 1 部分:https://www.youtube.com/watch?v=qAf9axsyijY&ab_channel=Blackthornprod)为我的代码制作了一个骨架,但我遇到了地面瓷砖生成重叠的问题。我在网上尝试了一些修复,但没有完全完成工作。
Physics.Overlap 使代码工作得更好,但仍然有规律的重叠。我怀疑重叠的瓷砖可能在完全相同的实例中产生,这使得 Overlap 命令错误地返回 0 colliders,但我不知道如何测试或避免它。
这是我的代码。它几乎是从上面的视频直接复制的。:
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],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],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],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;
}
}
}
spawn point prefab that code is attached to
我假设多次调试尝试的代码中有一些冗余,所以如果我能简化一些事情,请lmk。
解决方法
对视频的快速分析显示没有迹象表明原作者使用协程来生成新图块。
对于您生成的每个图块,您检查是否有重叠,然后等待 0.2 秒。正是在那里等待很容易让不同的瓷砖也检查相同的空间并发现没有瓷砖重叠。因此,两个或多个瓦片将看不到重叠,并且都写入相同的位置。
我是否可以建议清理代码,删除强制延迟并尝试这样做:
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;
}
}
,
我在此线程中在米兰的帮助下获得了代码。使用他的代码后。我使用序列化字段查看未满足哪些条件,并发现 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,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,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");
}
}
}