Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question Destroying an Object with no collision?

Discussion in 'Scripting' started by felixblome, Mar 16, 2021.

  1. felixblome

    felixblome

    Joined:
    Mar 8, 2021
    Posts:
    5
    Hey, I've struggling for several hours with this issue and now decided to ask for help. I will provide my scripts and screenshots to give the best possible overview.

    I am working on a dungeon generator which works pretty good, but there are two main issues. I have these dead ends (https://prnt.sc/10nicv8) which allow the player to enter the Gamescene. To tackle that, I generate a box that blocks these openings (https://prnt.sc/10nidxl), but this box creates the following problems:

    1. I can't set a maximum number of spawned rooms. The box is being instanced from the same script as the other rooms, which means, that the maximum number also applies to the box. This is extremely annoying to me and I don't know how to solve that.
    2. It's not clean at all. It would be sooo much better if the wrong rooms with openings (all rooms are prefabs) would destroy and replace themselves. I already tried working with raycasts but I didn't manage to fix it.
    Before posting here I really wanted to solve these issues by myself, but I am at the very beginning of programming with C# and Unity so my knowledge and experience is very limited which makes it really difficult to me.

    I am using the following scripts:

    [RoomSpawner -> spawns rooms and the box]

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomSpawner : MonoBehaviour
    6. {
    7.     public int openingDirection;
    8.  
    9.     /*
    10.         1 -> need bottom door
    11.         2 -> need top door
    12.         3 -> need left door
    13.         4 -> need right door
    14.     */
    15.  
    16.     private RoomTemplates templates;
    17.     private int random;
    18.     public bool spawned = false;
    19.  
    20.  
    21.     static int maxRooms;
    22.    
    23.  
    24.  
    25.     void Start ()
    26.     {
    27.         templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
    28.         StartCoroutine(SpawnRooms());
    29.     }
    30.  
    31.  
    32.      IEnumerator SpawnRooms()
    33.     {
    34.         yield return new WaitForSeconds(0.1f);
    35.         Spawn();
    36.     }
    37.    
    38.  
    39.     IEnumerator FixOpenRooms()
    40.     {
    41.  
    42.                 yield return new WaitForSeconds(1.5f);
    43.                 Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
    44.                 Destroy(gameObject);
    45.                 Debug.Log("Fixed!");
    46.    
    47.  
    48.        
    49.     }
    50.  
    51.  
    52.  
    53.     void Spawn ()
    54.     {
    55.  
    56.         if (spawned == false) // && maxRooms < 15)
    57.         {
    58.  
    59.             if (openingDirection == 1)             // Spawn Bottom Door
    60.  
    61.             {
    62.                 random = Random.Range(0, templates.bottomRooms.Length);
    63.                 Instantiate(templates.bottomRooms[random], transform.position, Quaternion.identity);
    64.                 //maxRooms++;
    65.             }
    66.             else if (openingDirection == 2)        // Spawn Top Door
    67.  
    68.             {
    69.                 random = Random.Range(0, templates.topRooms.Length);
    70.                 Instantiate(templates.topRooms[random], transform.position, Quaternion.identity);
    71.                 //maxRooms++;
    72.  
    73.             }
    74.             else if (openingDirection == 3)        // Spawn Left Door
    75.  
    76.             {
    77.                 random = Random.Range(0, templates.leftRooms.Length);
    78.                 Instantiate(templates.leftRooms[random], transform.position, Quaternion.identity);
    79.                 //maxRooms++;
    80.  
    81.             }
    82.             else if (openingDirection == 4)        // Spawn Right Door
    83.  
    84.             {
    85.                 random = Random.Range(0, templates.rightRooms.Length);
    86.                 Instantiate(templates.rightRooms[random], transform.position, Quaternion.identity);
    87.                 //maxRooms++;
    88.  
    89.  
    90.             }
    91.  
    92.             spawned = true;
    93.         }
    94.  
    95.      
    96.     }
    97.  
    98.    void OnTriggerEnter2D(Collider2D other)
    99.     {
    100.         if(other.CompareTag("SpawnPoint"))
    101.         {
    102.             if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
    103.             {
    104.  
    105.                 StartCoroutine(FixOpenRooms());
    106.  
    107.             }
    108.             spawned = true;
    109.  
    110.         }
    111.     }
    112.    
    113.  
    114.  
    115. }
    116.  
    [RoomTemplates -> Array that contains all Rooms]

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomTemplates : MonoBehaviour
    6. {
    7.     public GameObject[] bottomRooms;
    8.     public GameObject[] topRooms;
    9.     public GameObject[] leftRooms;
    10.     public GameObject[] rightRooms;
    11.  
    12.     public GameObject closedRoom;
    13. }
    14.  
    [Destroyer -> Prevents the box from spawning at the entry room]

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Destroyer : MonoBehaviour
    6. {
    7.  
    8.     private RoomTemplates templates;
    9.  
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if (!other.CompareTag("Player") && !other.CompareTag("ClosedRoom"))
    13.         {
    14.  
    15.             templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
    16.             Destroy(other.gameObject);
    17.  
    18.         }
    19.     }
    20. }
    21.  
    I appreciate any kind of help! Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    First of all, I love it. These sorts of things are awesome fun to code up.

    BUT... I have to warn you, some of the laying-out logic can be tricky.

    Looking through your scripts it will not be possible to reason about what you want vs what you are getting. That will have to be done by you, but here are some ideas:

    - make the dungeon SUPER small, whatever the smallest thing is that has a chance of showing your problem.

    - try to emit lots of Debug.Log() info about what it's doing at each step.

    - see if you can find the logic issue, IF that is what it is. Keep in mind it might just be a spatial issue with how you are generating the dungeon that does not account for dead ends properly, and may not ultimately be able to the way it is written.

    Finally let me leave you a link to this fellow, who has written some interesting bits on dungeon generation.

    https://journal.stuffwithstuff.com/2014/12/21/rooms-and-mazes/

    There are plenty other writers out on the interwebs, and the art and science of dungeon generation spans mathematics of graph theory, all the way to the artistic "coolness" factor that you feel when you enter it.
     
    seejayjames likes this.
  3. felixblome

    felixblome

    Joined:
    Mar 8, 2021
    Posts:
    5
    I agree, but it is also really difficult as a beginner haha!

    First of all, thanks for your response and the link, I will have a look at it right after this post.

    I acutally re-wrote the whole system and made it way simpler. Instead of my complex room-cluster that consisted of 14 different rooms with opening, I am now using an own grid system which sets a new coordinate for each room spawned.

    The doors are being created by shooting a raycast to the outside of the box, and if it collides with another object tagged as "Door", it destroys this object.

    My new system is so much easier to read and handle, it's really awesome! Now I want to figure out how to spawn different sized rooms, without making them collide with each other- I am wondering if I can figure out a working method.
     
    Kurt-Dekker likes this.