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 MadmanSilver · Mar 02, 2015 at 12:51 PM · randomproceduralgenerationtilesoverlapping

Overlapping Tile Generation

Ok, so for some reason that I cannot figure out, my generation script keeps overlapping itself. It is a very basic tile based generation script, but every time I run it, it spawns a tile where one already exists. If anybody can figure out why, please do.

Generator:

 public class Generator : MonoBehaviour {
     
     public GameObject tile;
     public bool done = false;
     public int size = 10;
 
     private GridCords origin = new GridCords(0, 0);
     private GridCords current;
     private GridCords last;
     public List<GridCords> hist = new List<GridCords>();
     private GridCords[] choices = new GridCords[4];
     private GameObject newTile;
     private bool first = true;
     private int past = 0;
 
     // Initialize stuff.
     public void Start() {
         current = origin;
         last = origin;
         choices[0] = new GridCords(last.GetX() + 1, last.GetY());
         choices[1] = new GridCords(last.GetX() - 1, last.GetY());
         choices[2] = new GridCords(last.GetX(), last.GetY() + 1);
         choices[3] = new GridCords(last.GetX(), last.GetY() - 1);
     }
     
     public void Generate() {
         if (size < 1) {
             done = true;
             return;
         }
 
         if (size == 9) {
             first = false;
         }
 
         if (first) {
             newTile = (GameObject)Instantiate(tile, origin.ToVect(), tile.transform.rotation);
             hist.Add(origin);
             last = origin;
             size--;
         } else {
             // Wait a bit.
             //Wait(2.0f);
             
             // Prepare for generation.
             choices[0] = new GridCords(last.GetX() + 1, last.GetY());
             choices[1] = new GridCords(last.GetX() - 1, last.GetY());
             choices[2] = new GridCords(last.GetX(), last.GetY() + 1);
             choices[3] = new GridCords(last.GetX(), last.GetY() - 1);
             
             // Generate.
             foreach (GridCords gc in choices) {
                 if (hist.Contains(gc)) {
                     past++;
                 }
             }
 
             if (past == 4) {
                 done = true;
                 return;
             }
 
             current = choices[Random.Range(0, 3)];
 
             while (hist.Contains(current)) {
                 current = choices[Random.Range(0, 3)];
             }
 
             newTile = (GameObject)Instantiate(tile, current.ToVect(), tile.transform.rotation);
             
             // Post-generation.
             hist.Add(current);
             last = current;
             size--;
             past = 0;
         }
     }
 
     private IEnumerator Wait(float secs) {
         yield return new WaitForSeconds(secs);
     }
 }


And here is the GridCords script:

 public class GridCords {
 
     private int x, y;
 
     public GridCords(int x, int y) {
         this.x = x;
         this.y = y;
     }
 
     // Creates world co-ordinates using the grid co-ordinates specified.
     public Vector3 ToVect() {
         return new Vector3(y * 10, 0, x * 10);
     }
 
     public int GetX() {
         return x;
     }
 
     public int GetY() {
         return y;
     }
 }

This class is mainly for a bit of organization in the code.

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

2 People are following this question.

avatar image avatar image

Related Questions

Tile-based Generation Not Working? 1 Answer

how to procedurally generate submarines with random events on the sub? 0 Answers

Procedural Generation... 2 Answers

Procedural Random Walk Dungeon Generator? 2 Answers

Procedural generation of dungeons with rooms made in the new tile map system? 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