I have 2 scripts one that check if an area is free one that spawn an object if the area is free this is to prevent overlapping checker script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpaceCheck : MonoBehaviour
{
public bool isFree = true;
public GameObject RoomParent;
void OnCollisionEnter(Collision col)
{
isFree = false;
}
void Start()
{
if(isFree == true)
{
RoomParent.SetActive(true);
}
}
void Update()
{
}
}
spawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomSpawner : MonoBehaviour
{
public List<Transform> Spawners;
public List<GameObject> Rooms;
int choosenSpawner = 0;
public GameObject SpaceCheck;
void Start()
{
choosenSpawner = Random.Range(0, Spawners.Count);
GameObject manager = GameObject.Find("RoomsManager");
RoomsManager managerProperty = manager.GetComponent<RoomsManager>();
GameObject Space = SpaceCheck;
SpaceCheck spaceProperty = Space.GetComponent<SpaceCheck>();
if (managerProperty.maxRoom > managerProperty.RoomCount && spaceProperty.isFree == true)
{
Instantiate(Rooms[Random.Range(0, Rooms.Count)], Spawners[choosenSpawner].position, Spawners[choosenSpawner].rotation);
RoomsManager.instance.AddRoomCount();
}
}
void Update()
{
}
}
Problem is that they still overlaps
here is the video of the problem: https://www.youtube.com/watch?v=mUjMUSNhDcQ