Search Unity

Procedural generation issue

Discussion in 'World Building' started by CyplexGaming, Aug 31, 2022.

  1. CyplexGaming

    CyplexGaming

    Joined:
    Aug 24, 2019
    Posts:
    4
    I have a script that randomly generates rooms as you go. When you enter a new room, a new one spawns ahead of it. however, there are rooms that can spawn others to the left and right, and if you get enough of these in a row it overlaps with already generated rooms. How do i fix this?

    code that detects when you enter a new room:
    Code (CSharp):
    1. private void OnTriggerEnter(Collider col){
    2.         if (col.gameObject.tag == "RoomTrigger"){
    3.             Room room = col.gameObject.GetComponentInParent<Room>();
    4.             Destroy(col.gameObject);
    5.             Debug.Log("Trigger");
    6.             GenerationManager.Instance.EnterRoom(room);
    7.         }
    8.     }
    code that spawns rooms at all spawnpoints for the next room:
    Code (CSharp):
    1. public void EnterRoom(Room newRoom){
    2.         currentRoom = newRoom.GetComponent<Room>();
    3.         roomNumber++;
    4.         for (int i = 0; i < currentRoom.nextRoomPoints.Length; i++){
    5.             Instantiate(rooms[Random.Range(0, rooms.Length)], currentRoom.nextRoomPoints[i].position, currentRoom.nextRoomPoints[i].rotation);
    6.         }
    7.      
    8.     }
    I was thinking that i could maybe try to check if a nextRoomPoint(a point where a new room can be created) is inside another room but I dont how to do that

    thoughts?
     
    Last edited: Sep 1, 2022