Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JayOhhh · Aug 09, 2014 at 07:33 AM · proceduralvector mathinsantiatevector 3

Procedural Generation - Rooms spawning at same positions.

Hello,

I've been working on my procedural generation for quite some time now. I'm having a bit of trouble, and I think it might be the method that I am using to determine where to spawn my next room. I'm currently using vector math, and I think this might be the wrong way...I'm wondering/hoping if you have a better idea. If you can point out what I'm doing wrong in this massive method, I would be greatly appreciated.

The problem seems to spawn when I am spawning a branch, some of the rooms are spawning on top of each other, when I dont think they should be!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DSSpawn : MonoBehaviour {
 
 public GameObject RoomPre;
 
 private Vector3 doorPosition;
 private Vector3 branchRoomSpawnPosition;
 
 private int roomCount;
 private int northDoor;
 private int southDoor;
 private int eastDoor;
 private int westDoor;
 private int currentBranchCount;
 private int branchLimit = 3;
 private int sizeOfList;
 
 private bool forceNorth;
 private bool forceSouth;
 private bool forceWest;
 private bool forceEast;
 
 private string roomName;
 
 private List<string> doorList = new List<string>();
     
 Dictionary<int, List<string>> roomUnattachedDoors = new Dictionary<int, List<string>>();
 Dictionary<int, Vector3> roomPosition = new Dictionary<int, Vector3>();
 Dictionary<int, List<string>> branchUnattachedDoors = new Dictionary<int, List<string>>();
 Dictionary<int, Vector3> branchRoomPosition = new Dictionary<int, Vector3>();
 
     void Update () {
         if(Input.GetKeyDown ("space")) {
             SpawnDungeon();
         }
     }
     // x + 40 = right spawn
     // x - 40 = left spawn
     // y + 21 = up spawn
     // y - 21 = down spawn
 
     void SpawnDungeon() {
 
         doorPosition = new Vector3(0,0,0);        
         
         //Spawn inital room
         DoorSpawn();
         Instantiate (RoomPre, doorPosition, RoomPre.transform.rotation);  
         roomCount = 1;
         roomUnattachedDoors.Add (roomCount, new List<string>(doorList));
         roomPosition.Add (roomCount, doorPosition);
         doorList.Clear();
         
         // Begin loop to create branches for each unattached door.
         List<string> unattachedMainRoom = roomUnattachedDoors[1];
         //List<string> doorsToRemove = roomUnattachedDoors[1];
         foreach (string unattachedDoor in unattachedMainRoom) {
             //Debug.Log ("unattached door loop");
             //doorsToRemove.Remove (unattachedDoor);
             //roomUnattachedDoors[1] = new List<string>(doorsToRemove);
             currentBranchCount = 1;
             doorPosition = new Vector3(0,0,0);
             while (currentBranchCount <= branchLimit) {
                 //Debug.Log ("currentBranchCount loop");    
 
                 
                 // Here I need if logic to get 2nd iteration of loop, if 2nd iteration, need to grab an unattached door from last spawned room and spawn next room appropriately.
                 // if currentBranchCount > 1 logic below, else grab unattached doors from list and grab one at random. use that to spawn next. repeat.
                 if (currentBranchCount < 2 ) {
 
                     if (unattachedDoor == "northDoorPre") {
                         forceSouth = true;
                         doorPosition.y = doorPosition.y + 21;
                     } else if (unattachedDoor == "southDoorPre") {
                         forceNorth = true;
                         doorPosition.y = doorPosition.y + -21;
                     } else if (unattachedDoor == "westDoorPre") {
                         forceEast = true;
                         doorPosition.x = doorPosition.x + -40;
                     } else if (unattachedDoor == "eastDoorPre") {
                         forceWest = true;
                         doorPosition.x = doorPosition.x + 40;
                     }
                     Debug.Log("First branch loop position = " + doorPosition);
                     roomCount = roomCount + 1;
                     roomPosition.Add (roomCount, doorPosition);
                     DoorSpawn();
                     Instantiate (RoomPre, doorPosition, RoomPre.transform.rotation);
                     roomUnattachedDoors.Add (roomCount, new List<string>(doorList));
                     branchUnattachedDoors.Add(currentBranchCount, new List<string>(doorList));
                     branchRoomPosition.Add(currentBranchCount, doorPosition);
                     doorList.Clear ();
                     currentBranchCount = currentBranchCount + 1;                
                  } else {
                     List<string> previousSpawned = new List<string>();
                     previousSpawned = roomUnattachedDoors[roomCount];
                     sizeOfList = previousSpawned.Count;
                     roomName = previousSpawned[Random.Range (0, sizeOfList)];
                     
                     if (roomName == "northDoorPre") {
                         forceSouth = true;
                         doorPosition.y = doorPosition.y + 21;
                     } else if (roomName == "southDoorPre") {
                         forceNorth = true;
                         doorPosition.y = doorPosition.y + -21;
                     } else if (roomName == "westDoorPre") {
                         forceEast = true;
                         doorPosition.x = doorPosition.x + -40;
                     } else if (roomName == "eastDoorPre") {
                         forceWest = true;
                         doorPosition.x = doorPosition.x + 40;
                     }
                     Debug.Log("Next Branch Loop position = " + doorPosition);
 
                     roomCount = roomCount + 1;
                     roomPosition.Add (roomCount, doorPosition);
                     DoorSpawn();
                     Instantiate (RoomPre, doorPosition, RoomPre.transform.rotation);
                     branchUnattachedDoors.Add(currentBranchCount, new List<string>(doorList));
                     branchRoomPosition.Add(currentBranchCount, doorPosition);
                     roomUnattachedDoors.Add (roomCount, new List<string>(doorList));
                     doorList.Clear ();
                     currentBranchCount = currentBranchCount + 1;
                   }
             } // End while loop for branch spawning
             
            currentBranchCount = 1;
            foreach (List<string> unattachedDoors in branchUnattachedDoors.Values) {              
                foreach (string unattached in unattachedDoors) {
                    branchRoomSpawnPosition = branchRoomPosition[currentBranchCount];
                    if (unattached == "northDoorPre") {
                        forceSouth = true;
                        forceWest = false;
                        forceEast = false;
                        forceNorth = false;
                            branchRoomSpawnPosition.y = branchRoomSpawnPosition.y + 21;
                    } else if (unattached == "southDoorPre") {
                        forceNorth = true;
                        forceSouth = false;
                        forceWest = false;
                        forceEast = false;
                        branchRoomSpawnPosition.y = branchRoomSpawnPosition.y + -21;
                    } else if (unattached == "westDoorPre") {
                        forceEast = true;
                        forceNorth = false;
                        forceSouth = false;
                        forceWest = false;
                     branchRoomSpawnPosition.x = branchRoomSpawnPosition.x + -40;
                    } else if (unattached == "eastDoorPre") {
                        forceWest = true;
                        forceNorth = false;
                        forceSouth = false;
                        forceEast = false;                        
                        branchRoomSpawnPosition.x = branchRoomSpawnPosition.x + 40;
                    }
                    DoorSpawn();
                    Instantiate(RoomPre, branchRoomSpawnPosition, transform.rotation);                   
                }
                currentBranchCount = currentBranchCount + 1;
            } // End loop for closing branch rooms
            branchUnattachedDoors.Clear();
            branchRoomPosition.Clear();
         } // End main loop for initial room unattached        
     }
 
     void DoorSpawn() {
         
         northDoor = Random.Range (0,2);
         southDoor = Random.Range (0,2);        
         eastDoor = Random.Range (0,2);        
         westDoor = Random.Range (0,2);
 
         if (forceNorth)
         {
             northDoor = 1;
         }
         if (forceSouth)
         {
             southDoor = 1;
         }
         if (forceWest)
         {
             westDoor = 1;
         }
         if (forceEast)
         {
             eastDoor = 1;
         }
 
         if (northDoor == 0 && southDoor == 0 && westDoor == 0 && eastDoor == 0)
         {
             int random = Random.Range(0, 5);
             if (random == 1)
             {
                 northDoor = 1;
             } else if (random == 2) {
                 southDoor = 1;
             }
             else if (random == 3)
             {
                 westDoor = 1;
             }
             else if (random == 4)
             {
                 eastDoor = 1;
             }
         }
         
         SpriteRenderer[] doors = RoomPre.GetComponentsInChildren<SpriteRenderer>(true);
         
         foreach (SpriteRenderer renderer in doors) {
             if (renderer.name == "northDoorPre") {
                 if (northDoor == 1) {
                     renderer.enabled = true;
                     if (!forceNorth)
                     {
                         doorList.Add(renderer.name);
                     }
                     forceNorth = false;
                 } else {
                     renderer.enabled = false;
                 }
             } else if (renderer.name == "southDoorPre") {
                 if (southDoor == 1) {                    
                     renderer.enabled = true;
                     if (!forceSouth)
                     {
                         doorList.Add(renderer.name);
                     }
                     forceSouth = false;
                 } else {
                     renderer.enabled = false;
                 }
                 
             } else if (renderer.name == "westDoorPre") {
                 if (westDoor == 1) {
                     renderer.enabled = true;
                     if (!forceWest)
                     {
                         doorList.Add(renderer.name);
                     }
                     forceWest = false;
                 } else {
                     renderer.enabled = false;
                 }
             } else if (renderer.name == "eastDoorPre") {
                 if (eastDoor == 1) {
                     renderer.enabled = true;
                     if (forceEast)
                     {
                         doorList.Add(renderer.name);
                     }
                     forceEast = false;
                 } else {
                 renderer.enabled = false;
                 }
             } 
         } // End foreach loop
     } // End door Spawn
 }
 

Appologies for the massive code post, I'm sure you don't want to read through it all! I figured i'd post the entire method so you can get the hang of what i'm doing.

I believe the problem is down at the lines where im doing doorPosition.x + and doorPosition.y +. I think this is a problem, but I can't seem to think of a way to position the room in the appropriate direction.

I am able to tell which direction it needs to go by the unattached room I am looping through. But how do I actually instantiate it there without a vector 3? Is there a spawn right method or something I'm not aware of, is my math off or something?

Any help is muchly appreciated!!

Thanks,

JayOhh Games.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Graphics.DrawProcedural from OnRenderObject 1 Answer

How can I outline selected cells on a grid? 1 Answer

Using Unity to trigger MIDI sequences with Wwise 0 Answers

How do i assign perlin noise 2 Answers

Substance - How do you set the random seed of procedural material at runtime 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