Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 damagingwazza · Jun 04, 2019 at 08:19 PM · generationgeneratorrandom gen

Do anyone know how im able to check if there is duplicate of the other object, than if there is change it to a different object?

I'm creating this 2D random dungeon generator and I'm having trouble trying to figure out how I can make the instance of the tileset not duplicate each other like the photo below.alt text

In this picture is an example that on the side walls to the left there is a corner that duplicates itself all through the way instead of being the sidewall. And that's what I'm trying to fix.

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class LeveGenerator : MonoBehaviour
 {
     
     enum gridSpace { empty, floor, wall ,sidewallR, sidewallL, CornerTopLeft, CornerTopRight, CornerBottomLeft, CornerBottomRight, BottomWall,Player };
     
     gridSpace[, ] grid;
     public int roomHeight, roomWidth;
     [Header("Room Settings")]
     public Vector2 roomSizeWorldUnits = new Vector2(30, 30);
     public float worldUnitsInOneGridCell = 1;
     public struct walker
     {
         public Vector2 dir;
         public Vector2 pos;
     }
     public List<walker> walkers;
     public float chanceWalkerChangeDir = 0.5f, chanceWalkerSpawn = 0.05f;
     public float chanceWalkerDestoy = 0.05f;
     public int maxWalkers = 10;
     public float percentToFill = 0.2f;
 
     public int spawnNum = 0;
 
     private Replace rp;
     private LeveGenerator Gen;
 
     [Header("Object Spawn Rate")]
     public float chanceToSpawnEnemy = 0.2f;
     public float chanceToSpawnItem = 0.5f;
     public GameObject Enemy;
     public GameObject Items;
 
 
     [Header("Object Tileset")]//setup tile sets
     public GameObject wallObj;
     public GameObject floorObj;
     public GameObject sidewallR;
     public GameObject sidewallL;
     public GameObject CornerTopLeft;
     public GameObject CornerTopRight;
     public GameObject CornerBottomLeft;
     public GameObject CornerBottomRight;
     public GameObject BottomWall;
     
 
     [Header("Player")]
     public GameObject Player;
 
 
    
     void Start()
     {
         Setup();
         CreateFloors();
         CreateWalls();
         RemoveSingleWalls();
         //RemoveSideWall();
         SpawnLevel();
         SpawnObjects();
         SpawnPlayer();
         
         
 
     }
 
     public void SpawnPlayer() {
         for (int x = 0; x < roomWidth; x++)
         {
             for (int y = 0; y < roomHeight; y++)
             {
                 if (grid[x, y] == gridSpace.floor && spawnNum < 1)
                 {
                     spawnNum++; //spawns player once
                     Spawn(x, y, Player);
                 }
             }
         }
     }
 
     public void SpawnObjects() {
         for (int x = 0; x < roomWidth; x++)
         {
             for (int y = 0; y < roomHeight; y++)
             {
                 if(grid[x, y] == gridSpace.floor &&
                    Random.value < chanceToSpawnEnemy){
                     Spawn(x, y, Enemy);
                 }
             }
         }
         for (int x = 0; x < roomWidth; x++)
         {
             for (int y = 0; y < roomHeight; y++)
             {
                 if (grid[x, y] == gridSpace.floor &&
                    Random.value < chanceToSpawnItem)
                 {
                     Spawn(x, y, Items);
                 }
             }
         }
     }
 
     public void Setup()
     {
         //find grid size
         roomHeight = Mathf.RoundToInt(roomSizeWorldUnits.x / worldUnitsInOneGridCell);
         roomWidth = Mathf.RoundToInt(roomSizeWorldUnits.y / worldUnitsInOneGridCell);
         //create grid
          grid = new gridSpace[roomWidth, roomHeight];
         //set grid's default state
         for (int x = 0; x < roomWidth - 1; x++)
         {
             for (int y = 0; y < roomHeight - 1; y++)
             {
                 //make every cell "empty"
                 grid[x, y] = gridSpace.empty;
             }
         }
         //set first walker
         //init list
         walkers = new List<walker>();
         //create a walker 
         walker newWalker = new walker();
         newWalker.dir = RandomDirection();
         //find center of grid
         Vector2 spawnPos = new Vector2(Mathf.RoundToInt(roomWidth / 2.0f),
                                         Mathf.RoundToInt(roomHeight / 2.0f));
         newWalker.pos = spawnPos;
         //add walker to list
         walkers.Add(newWalker);
     }
     public void CreateFloors()
     {
         int iterations = 0;//loop will not run forever
         do
         {
             //create floor at position of every walker
             foreach (walker myWalker in walkers)
             {
                grid[(int)myWalker.pos.x, (int)myWalker.pos.y] = gridSpace.floor;
             }
             //chance: destroy walker
             int numberChecks = walkers.Count; //might modify count while in this loop
             for (int i = 0; i < numberChecks; i++)
             {
                 //only if its not the only one, and at a low chance
                 if (Random.value < chanceWalkerDestoy && walkers.Count > 1)
                 {
                     walkers.RemoveAt(i);
                     break; //only destroy one per iteration
                 }
             }
             //chance: walker pick new direction
             for (int i = 0; i < walkers.Count; i++)
             {
                 if (Random.value < chanceWalkerChangeDir)
                 {
                     walker thisWalker = walkers[i];
                     thisWalker.dir = RandomDirection();
                     walkers[i] = thisWalker;
                 }
             }
             //chance: spawn new walker
             numberChecks = walkers.Count; //might modify count while in this loop
             for (int i = 0; i < numberChecks; i++)
             {
                 //only if # of walkers < max, and at a low chance
                 if (Random.value < chanceWalkerSpawn && walkers.Count < maxWalkers)
                 {
                     //create a walker 
                     walker newWalker = new walker();
                     newWalker.dir = RandomDirection();
                     newWalker.pos = walkers[i].pos;
                     walkers.Add(newWalker);
                 }
             }
             //move walkers
             for (int i = 0; i < walkers.Count; i++)
             {
                 walker thisWalker = walkers[i];
                 thisWalker.pos += thisWalker.dir;
                 walkers[i] = thisWalker;
             }
             //avoid boarder of grid
             for (int i = 0; i < walkers.Count; i++)
             {
                 walker thisWalker = walkers[i];
                 //clamp x,y to leave a 1 space boarder: leave room for walls
                 thisWalker.pos.x = Mathf.Clamp(thisWalker.pos.x, 1, roomWidth - 2);
                 thisWalker.pos.y = Mathf.Clamp(thisWalker.pos.y, 1, roomHeight - 2);
                 walkers[i] = thisWalker;
             }
             //check to exit loop
             if ((float)NumberOfFloors() / (float)grid.Length > percentToFill)
             {
                 break;
             }
             iterations++;
         } while (iterations < 100000);
     }
     
     public void CreateWalls()
     {
        
         //loop though every grid space
         for (int x = 0; x < roomWidth - 1; x++)
         {
             for (int y = 0; y < roomHeight - 1; y++)
             {
                 //if theres a floor, check the spaces around it
                 if (grid[x, y] == gridSpace.floor)//
                 {
                     //if any surrounding spaces are empty, place a wall
                     if (grid[x, y + 1] == gridSpace.empty)//TOP WALL
                     {
                         grid[x, y + 1] = gridSpace.wall;
                     }
                     if (grid[x, y - 1] == gridSpace.empty)//BOTTOM WALL
                     {
                         grid[x, y - 1] = gridSpace.wall;
                     }
                     if (grid[x + 1, y] == gridSpace.empty)//SIDEWALL RIGHT
                     {
                         grid[x + 1, y] = gridSpace.sidewallR;
                     }
 
                     if (grid[x - 1, y] == gridSpace.empty)//SIDEWALL LEFT 
                     {
                         grid[x - 1, y] = gridSpace.sidewallL;
                     }
 
                     if (grid[x - 1, y - 1] == gridSpace.empty )//Bottom Coner left
                     {
                         grid[x - 1, y - 1] = gridSpace.CornerBottomLeft;
                     }
 
 
                     //working on this
                     if (grid[x - 1, y + 1] == gridSpace.empty)
                     {
                         grid[x - 1, y + 1] = gridSpace.CornerTopLeft;
                     }
 
 
                 }
             }
         }
     }
     
     
     public void SpawnLevel()
     {
         for (int x = 0; x < roomWidth; x++)
         {
             for (int y = 0; y < roomHeight; y++)
             {
                 switch (grid[x, y])
                 {
                     case gridSpace.empty:
                         break;
                     case gridSpace.floor:
                         Spawn(x, y, floorObj);
                         break;
                     case gridSpace.wall:
                         Spawn(x, y, wallObj);
                         break;
                     case gridSpace.sidewallR:
                         Spawn(x, y, sidewallR);
                         break;
                     case gridSpace.sidewallL:
                         Spawn(x, y, sidewallL);
                         break;
                     case gridSpace.CornerBottomLeft:
                         Spawn(x, y, CornerBottomLeft);
                         break;
                     case gridSpace.CornerTopLeft:
                         Spawn(x, y, CornerTopLeft);
                         break;
                     
                 }
             }
         }
     }
 
     public void RemoveSingleWalls()
     {
         //loop though every grid space
         for (int x = 0; x < roomWidth - 1; x++)
         {
             for (int y = 0; y < roomHeight - 1; y++)
             {
                 //if theres a wall, check the spaces around it
                 if (grid[x, y] == gridSpace.wall)
                 {
                     //assume all space around wall are floors
                     bool allFloors = true;
                     //check each side to see if they are all floors
                     for (int checkX = -1; checkX <= 1; checkX++)
                     {
                         for (int checkY = -1; checkY <= 1; checkY++)
                         {
                             if (x + checkX < 0 || x + checkX > roomWidth - 1 ||
                                 y + checkY < 0 || y + checkY > roomHeight - 1)
                             {
                                 //skip checks that are out of range
                                 continue;
                             }
                             if ((checkX != 0 && checkY != 0) || (checkX == 0 && checkY == 0))
                             {
                                 //skip corners and center
                                 continue;
                             }
                             if (grid[x + checkX, y + checkY] != gridSpace.floor)
                             {
                                 allFloors = false;
                             }
                         }
                     }
                     if (allFloors)
                     {
                         grid[x, y] = gridSpace.floor;
                     }
                 }
             }
         }
     }
     /*
     public void RemoveSideWall()
     {
         //loop though every grid space
         for (int x = 0; x < roomWidth - 1; x++)
         {
             for (int y = 0; y < roomHeight - 1; y++)
             {
                 //if theres a wall, check the spaces around it
                 if (grid[x, y] == gridSpace.sidewallL)
                 {
                     //assume all space around wall are floors
                     bool allFloors = true;
                     //check each side to see if they are all floors
                     for (int checkX = -1; checkX <= 1; checkX++)
                     {
                         for (int checkY = -1; checkY <= 1; checkY++)
                         {
                             if (x + checkX < 0 || x + checkX > roomWidth - 1 ||
                                 y + checkY < 0 || y + checkY > roomHeight - 1)
                             {
                                 //skip checks that are out of range
                                 continue;
                                 
                             }
                             if ((checkX != 0 && checkY != 0) || (checkX == 0 && checkY == 0))
                             {
                                 //skip corners and center
                                 //allFloors = true;
                                 continue;
                             }
                             if (grid[x + checkX, y + checkY] != gridSpace.floor)
                             {
                                 allFloors = true;
                             }
                         }
                     }
                     if (allFloors)
                     {
                         grid[x, y] = gridSpace.floor;
                     }
                 }
             }
         }
     }
     */
     public Vector2 RandomDirection()
     {
         //pick random int between 0 and 3
         int choice = Mathf.FloorToInt(Random.value * 3.99f);
         //use that int to chose a direction
         switch (choice)
         {
             case 0:
                 return Vector2.down;
             case 1:
                 return Vector2.left;
             case 2:
                 return Vector2.up;
             default:
                 return Vector2.right;
         }
     }
     int NumberOfFloors()
     {
         int count = 0;
         foreach (gridSpace space in grid)
         {
             if (space == gridSpace.floor)
             {
                 count++;
             }
         }
         return count;
     }
     public void Spawn(float x, float y, GameObject toSpawn)
     {
         //find the position to spawn
         Vector2 offset = roomSizeWorldUnits / 2.0f;
         Vector2 spawnPos = new Vector2(x, y) * worldUnitsInOneGridCell - offset;
         //spawn object
         Instantiate(toSpawn, spawnPos, Quaternion.identity);
     }
 
     public void Update()
     {
         if (Input.GetButtonDown("Jump")) {
             Restart();
         }
 
         
     }
 
     
 
     public void Restart()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 }

itchio.png (30.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Jun 04, 2019 at 08:59 PM

Okay so Imagine this layout:

 TTT
 TX2T
 TX1T
 TTT

Where T is empty, and X1 & X2 are floors.

So if we are currently on tile X1 and we check the topright corner of that tile so the T to the left of X2 will be a corner wall, doesn't make much sense right?

What you should do is get all the empty tiles and change that tile based on if the tiles around it are floors instead.

Also some advice, next time don't put such a large code in here. Put it in a hastebin instead.

Comment
Add comment · Share
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

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

107 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 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 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 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 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 avatar image avatar image

Related Questions

How can I make a Terraria Style game from sprites without using an enormous amount of gameobjects? 2 Answers

Terrain Toolkit - License 0 Answers

Generation Problem c# 0 Answers

Random generation problem. 2 Answers

Code Loops Indefinitely 1 Answer


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