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 awplays49 · Sep 30, 2015 at 09:36 PM · listgenerationobstaclequeue

Strange behavior with map connectivity

I have strange behavior with a map generator script.

This code below calls the code after it:

 void GenerateTiles (GameObject map) {
         tiles = new GameObject [mapWidth, mapHeight];
         towerHeights = new float [mapWidth, mapHeight];
         mapCenter = new Coord (mapWidth / 2, mapHeight / 2);
         int towerFrequency = (int) (mapWidth * mapHeight * towerPercent);
         int obstacleCount = 0;
         for (int i = 0; i < towerFrequency; i ++)
         {
             Coord coord = GetRandomCoord ();
             float height = Random.Range (minTowerHeight, maxTowerHeight);
             towerHeights [coord.x, coord.y] = height;
             if (height > 0.25f)
             {
                 obstacleCount ++;
             }
             if (!SweepMap (obstacleCount) || !CompareCoordinates (coord, mapCenter))
             {
                 height = 0.25f;
                 towerHeights [coord.x, coord.y] = height;
                 obstacleCount --;
             }    
             if (!CompareCoordinates (coord, mapCenter))
             {
                 GameObject newTower = Instantiate (tower, CoordToPosition (coord), Quaternion.identity) as GameObject;
                 newTower.transform.localScale = new Vector3 (1 - outlinePercentage, height, 1 - outlinePercentage);
                 newTower.transform.parent = map.transform;
                 tiles [coord.x, coord.y] = newTower;
             }
         }
         for (int x = 0; x < mapWidth; x ++)
         {
             for (int y = 0; y < mapHeight; y ++)
             {
                 if (tiles [x, y] == null)
                 {
                     GameObject newTile = Instantiate (tile, CoordToPosition (new Coord (x, y)), Quaternion.Euler (90, 0, 0)) as GameObject;
                     newTile.transform.localScale = new Vector3 (1 - outlinePercentage, 1 - outlinePercentage, 1);
                     newTile.transform.parent = map.transform;
                     tiles [x, y] = newTile;
                 }
             }
         }
     }

Code called at various times:

 bool SweepMap (int o) {
         bool [,] mapFlags = new bool [mapWidth, mapHeight];
         mapFlags [mapCenter.x, mapCenter.y] = true;
         Queue <Coord> queue = new Queue <Coord> ();
         queue.Enqueue (mapCenter);
         int accessibleTiles = 1;
         while (queue.Count > 0)
         {
             Coord tile = queue.Dequeue ();
             for (int x = -1; x < 2; x ++)
             {
                 for (int y = -1; y < 2; y ++)
                 {
                     int neighborX = tile.x + x;
                     int neighborY = tile.y + y;
                     if (x != 0 || y != 0)
                     {
                         if (neighborX > 0 && neighborX < mapWidth && neighborY > 0 && neighborY < mapHeight)
                         {
                             if (!mapFlags [neighborX, neighborY])
                             {
                                 if (!CheckNeighbor (neighborX, neighborY))
                                 {
                                     queue.Enqueue (new Coord (neighborX, neighborY));
                                     accessibleTiles ++;
                                 }
                                 mapFlags [neighborX, neighborY] = true;
                             }
                         }
                     }
                 }
             }
         }
         int targetTiles = mapWidth * mapHeight - o;
         if (accessibleTiles == targetTiles)
         {
             return true;
         }
         return false;
     }
     
     bool CheckNeighbor (int x, int y) {
         if (towerHeights [x, y] > 0.25f)
         {
             return true;
         }
         return false;
     }
     
     Coord GetRandomCoord () {
         Coord randomCoord = shuffledCoords.Dequeue ();
         shuffledCoords.Enqueue (randomCoord);
         return randomCoord;
     }
     
     Vector3 CoordToPosition (Coord coord) {
         float x = (float) (coord.x - mapWidth / 2);
         float y = (float) (coord.y - mapHeight / 2);
         return new Vector3 (x, 0.01f, y);
     }

What happens is all the towers are scaled down, meaning it thinks everything is a threat to an enclosed space. Any help is appreciated.

You can try the script by downloading this.

Comment
Add comment · Show 1
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
avatar image awplays49 · Oct 02, 2015 at 12:06 AM 0
Share

I just changed the code because I rewrote everything and no luck. I'm just commenting to let everyone know I changed the code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jomik · Oct 02, 2015 at 04:44 PM

Try debug your code and discover what part isn't run... It'd be good if you could add some comments so we know what you intend each section to do. Also make sure you tell us what variables are set to... Specifically on

 float height = Random.Range (minTowerHeight, maxTowerHeight);

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

28 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

Related Questions

A node in a childnode? 1 Answer

Queueing methods and calling them at specific times 1 Answer

C# - Is it possible to sync a Queue to a List, or is there a better option? 1 Answer

Creating List of Player 1 Answer

How do i go about generating stuff in editor and keeping it in play mode 2 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