
It looks like you're new here. If you want to get involved, click one of these buttons!
I am trying to take pockets of Perlin noise, and generate a dungeon inside of it, but to keep it to one pocket, I tried to make a self-duplicaating object that would spread thoughout and die out, without going back on itself. It would generate a sphere where it was that would delete any new objects. exept it doesn't work. I get an infinite loop that freezes up Unity and breaks.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine
public class RoomGen : MonoBehaviour
{
public float RoomValue;
public float seed = 1.457582f;
public GameObject Marker;
public GameObject Self;
// Start is called before the first frame update
void Start()
{
}
void LateUpdate()
{
RoomValue = Mathf.PerlinNoise(transform.position.x * seed, transform.position.z * seed);
if (RoomValue >= .4f)
{
Instantiate(Marker, transform.position, transform.rotation);
Instantiate(Self, transform.position + new Vector3(-1, 0 ,0), transform.rotation);
Instantiate(Self, transform.position + new Vector3(1, 0, 0), transform.rotation);
Instantiate(Self, transform.position + new Vector3(0, 0, -1), transform.rotation);
Instantiate(Self, transform.position + new Vector3(0, 0, 1), transform.rotation);
Destroy(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
private void OnCollisionStay(Collision collision)
{
Destroy(this.gameObject);
}
}
In theory the collison should trigger before the late update, then destroying the object before it can spread. In practice it all breaks down into an infinite loop. Any advice would be appreaciated
Comments
It might be an idea to run it in a coroutine with a delay on it. That way, you can more easily see what is causing the break.
@Sander I know where the problem is, but not how to fix it. The OnCollisionStay() is not triggering before the LateUpdate(), causing an inifnite loop