Reputation: 11
I am currently undertaking a University assignment in which I have to create a 2D Game. I have opted to go for a "Binding of Isaac" Style dungeon crawler. This issue began with the addition of box colliders to my room sprites. The dungeon generation is created by spawn points on each game object. There is a closed room which spawns when two spawn points collide and destroy before a room is spawned.
With the addition of colliders to the rooms, the spawn points are colliding with the rooms and destroying them. This happens specifically with the entry room due to the destroyer which is located there, as the spawn room often has 4 spawn points being generated on top of one another - so the destroyer removes these.
Below are my dungeon generation classes.
Room Spawner:
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
//So for a room with a door on the right, you will type 3, as a left door is needed in the next room to connect the two rooms.
private RoomTemplates templates;
private int rand;
private bool spawned = false;
private Vector3 entryPos;
void Start()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
Invoke("Spawn", 0.1f);
}
void Spawn()
{
if (spawned == false)
{
rand = Random.Range(0, templates.bottomRooms.Length);
if (openingDirection == 1)
{
//Need to spawn room with BOTTOM door
Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
}
else if (openingDirection == 2)
{
//Need to spawn room with TOP door
Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
}
else if (openingDirection == 3)
{
//Need to spawn room with LEFT door
Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
}
else if (openingDirection == 4)
{
//Need to spawn room with RIGHT door
Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
}
spawned = true;
}
}
void OnTriggerEnter2D(Collider2D other)
{
entryPos = templates.entryRoom.transform.position;
if (other.CompareTag("Spawn Point"))
{
if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false && transform.position != entryPos)
{
Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
}
spawned = true;
}
}
}
Room Templates:
using System.Collections.Generic;
using UnityEngine;
public class RoomTemplates : MonoBehaviour
{
public GameObject[] bottomRooms;
public GameObject[] topRooms;
public GameObject[] leftRooms;
public GameObject[] rightRooms;
public GameObject entryRoom;
public GameObject closedRoom;
public List<GameObject> rooms;
public float waitTime;
private bool spawnedBoss;
public GameObject boss;
private void Update()
{
if(waitTime <= 0 && spawnedBoss ==false) //if the wait time is 0 and boss isnt spawned a boss will spawn.
{
for (int i = 0; i < rooms.Count; i++)// will run as long as i is less than the no. of elements in list
{
if (i == rooms.Count - 1)//lists start with an index of zero
{
Instantiate(boss, rooms[i].transform.position, Quaternion.identity);//spawn boss at the room of index i's position
spawnedBoss = true; //stop bosses infinitly spawning
}
}
} else
{
waitTime -= Time.deltaTime; //slowly decrease wait time value
//we must wait before spawning boss to ensure all rooms have been spawned.
}
}
}
Add Room:
using System.Collections.Generic;
using UnityEngine;
public class AddRoom : MonoBehaviour
{
private RoomTemplates templates;
void Start()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
templates.rooms.Add(this.gameObject);
}
}
Destroyer:
using System.Collections.Generic;
using UnityEngine;
public class Destroyer : MonoBehaviour
{
private RoomTemplates templates;
void OnTriggerEnter2D(Collider2D other)
{
if(gameObject.tag == "Spawn Point")
{
Destroy(other.gameObject);
} else if(gameObject.tag != "Spawn Point")
{
Physics2D.IgnoreCollision(templates.entryRoom.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
}
}
Upvotes: 0
Views: 470
Reputation: 66
LayerMask might be the one you are looking for. You can keep gameobjects, that you don't want to collide, in different layers and change collision matrix (ProjectSettings > Physics2D > Layer Collision Matrix) so these objects don't collide with each other.
Upvotes: 2