Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lazyllama123 · Apr 27, 2016 at 05:21 AM · c#2dprocedural-generationdungeon

How to prevent overlapping GameObjects before instantiation

I have created a basic 2D dungeon generator script that lays down rooms in a random offset direction from the last placed room. I am struggling to come up with a way to check if there is already a room spawned in the place where a new room will spawn. Right now the rooms will go back on themselves and create large blobs of overlapping rooms, like this example with 2000 rooms generated:alt text

I have looked into different options on implementing some sort of ray casting solution where before I spawn the room I shoot a ray in the direction to check if there is a pre-existing room there and if there is it will choose a new direction. I confused of how I would implement this and any help would be appreciated. Here is the code that I am working with:

 using UnityEngine;
 using System.Collections;
 
 public class Generator : MonoBehaviour {
 
     //set up Gm's for generation
     public GameObject roomBlock;
     GameObject room;
     //set the number of rooms that will spawn in total
     public int numRooms = 8;
 
     float posx, posy, posz = 0;
 
     int randDirection;
 
     Quaternion rotation = Quaternion.Euler(270, 0, 0);
 
     void Start()
     {
         
         //Generation should only run through once at the start
         Generate(numRooms, roomBlock);
 
     }
 
     void Generate(int nrms, GameObject roomBlock)
     {
 
         //create the starting room at coords 0,0,0
         room = (GameObject)Instantiate(roomBlock, new Vector3(posx, posy, posz), rotation);
 
         //subtract 1 room from room pool
         nrms--;
 
 
         for (int i = 0; i < nrms; i++)
         {
             //get xyz of last spawned room
             posx = room.transform.position.x;
             posy = room.transform.position.y;
             posz = room.transform.position.z;
 
             //choose random direction (NSEW)
             randDirection = Random.Range(0, 4);
 
             //based on randomized direction spawn a room
             //spawn room in same position as last room but offset by 1 in the randomized direction
             if (randDirection == 0)
             {
                 room = (GameObject)Instantiate(roomBlock, new Vector3(posx + 1, posy, posz), rotation);
             }
             else if (randDirection == 1)
             {
                 room = (GameObject)Instantiate(roomBlock, new Vector3(posx, posy + 1, posz), rotation);
             }
             else if (randDirection == 2)
             {
                 room = (GameObject)Instantiate(roomBlock, new Vector3(posx - 1, posy, posz), rotation);
             }
             else if (randDirection == 3)
             {
                 room = (GameObject)Instantiate(roomBlock, new Vector3(posx, posy - 1, posz), rotation);
             }
 
 
         }
         
     }
 
 }


screenshot-2016-04-26-214843.png (124.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Button Enabling/Disabling using Collision Triggers? 1 Answer

2D platformer- getting errors I don't understand (c#) 1 Answer

How to make Comanion AI pass through the player??,How to make your Companion AI pass through the player?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges