Search code examples
c#unity-game-engine2dprocedural-generationroguelike

Align Spawnpoints in Procedural generation


I've created some boxes to recreate a 2D level where the dungeon is being created in a procedural generation. Altho when I run my script it creates the right levels, but it does not align the Spawnpoints to each other.

Standard tile with spawnpoints

This causes an unlimited level generation (since the spawn points don't touch) but it also makes sure that there is no way for the character to go from 1 way to the other. I've made sure that every level is 10 x 10 and the spawn points are 10 pixels away from the middle of the rooms. I wanted to ensure collision between the spawn points but unfortunately, this is not the case.

Spawnpoints not touching eachother

I have created multiple rooms where each has their own exit (Spawn Point) depending on where the spawn point is, it can connect with another room that has the same spawn point (Room with a left exit is connected by a room with at least a right exit).

I put all of this in an int, declaring the number of rooms that are available.

Roomspawner code


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

public class RoomSpawner : MonoBehaviour
{
    public int openingDirection;
    // 1 --> need bottom door
    // 2 --> need top door
    // 3 --> need left door
    // 4 --> need right door

    private RoomTemplates templates;
    private int rand;
    private bool spawned = false;

    void Start(){
      templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
      Invoke("Spawn", 0.5f);
    }

    void Spawn(){
      if(spawned == false){
        if(openingDirection == 1){
            // Need to spawn a room with a BOTTOM door.
            rand = Random.Range(0, templates.bottomRooms.Length);
            Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
          } else if(openingDirection == 2){
            // Need to spawn a room with a TOP door.
            rand = Random.Range(0, templates.topRooms.Length);
            Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
          } else if(openingDirection == 3){
            // Need to spawn a room with a LEFT door.
            rand = Random.Range(0, templates.leftRooms.Length);
            Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
          } else if(openingDirection == 4){
            // Need to spawn a room with a RIGHT door.
            rand = Random.Range(0, templates.rightRooms.Length);
            Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
          }
          spawned = true;
      }

      void OnTriggerEnter2D(Collider2D other){
        if(other.CompareTag("SpawnPoint")){
          if(other.GetComponent<RoomSpawner>().spawned == false && spawned == false){
            // spawns walls blocking off any opening !
            Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
            Destroy(gameObject);
          }
          spawned = true;
        }
    }
  }
}

Room templates


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

public class RoomTemplates : MonoBehaviour
{
    public GameObject[] bottomRooms;
    public GameObject[] topRooms;
    public GameObject[] leftRooms;
    public GameObject[] rightRooms;

    public GameObject closedRoom;
}

I did a new test with a room where it has only 1 spawn point. I Paused the game the second the 2nd spawn point was created so you can see that the 2nd level is created a with a different height than the starting one.

First room

Underneath you can see the connecting room spawning just above the starting one. By doing this it messes up all the rest of the rooms.

Second room different height

Instead I want it to be like this

Perfect room


Solution

  • It seems to me like the problem arises due to the way you instantiate the copies of the rooms.

    They are all instantiated at transform.position but given that they appear to spawn at all sorts of places, I'd suggest that you make sure that the pivot point/center point of the prefabs/meshes of your Room Templates are aligned correctly.