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 Bossquake1 · Aug 28, 2018 at 06:56 PM · positionrandomspawnintervalalong

How do i spawn a square in random position within an area but only at certain x and y intervals?

basically its a randomly generated topdown view tile map but i want the tiles to spawn at intervals of 0.64 so they dont overlap.

Heres my code: public class newMapGen : MonoBehaviour { public int tileTypes;

 public int maxX = 64;
 public int maxY = 64;

 bool runLoop = true;
 public int tilesss = 0;

 public System.Collections.Generic.List<GameObject> tiles = new System.Collections.Generic.List<GameObject>();
 private GameObject Grass;
 private GameObject Dirt;
 private GameObject Rock;
 private GameObject Gold;
 void Start()
 {
     {
         while (tilesss < 4096)
         {
             GameObject tileInstance;
             tileInstance = Instantiate(tiles[Random.Range(0, tileTypes)]) as GameObject;
             tileInstance.transform.localPosition = new Vector3(Random.Range(0f, 64f) * 0.64f, Random.Range(0f, 64f) * 0.64f, 0);
             tileInstance.transform.parent = transform;
             tilesss += 1;
         }
     }
 }

}

Any help is appreciated :)

Comment
Add comment · Show 4
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 Ramlock · Aug 28, 2018 at 07:22 PM 0
Share

You didn't actually ask a question, you just stated what you are trying to do and pasted your code. What is wrong or incomplete with it?

I'll point out, though, that you should always set the parent transform BEFORE you set a LocalPosition, otherwise it'll be set relative to the old parent, which is not intended. That means you should switch those lines, making the parent be set first.

Other than that, you never set your list up, and that's probably what you're perceiving as the problem. Try adding this directly above your "while" statement:

 tiles = new System.Collections.Generic.List<GameObject>() { Grass, Dirt, Rock, Gold };
avatar image Bossquake1 Ramlock · Aug 29, 2018 at 08:27 PM 0
Share

Hello sorry for not specifying my problem clearly, currently the tiles spawn totally randomly, overlapping. i want them to only spawn once every 0.64 units so they do not overlap. the list seems to work, as the tiles spawn but i will definitely try what you said.

avatar image Ramlock Bossquake1 · Aug 29, 2018 at 09:22 PM 0
Share

I only later realised you are setting the list from the inspector, and the 4 gameobjects are just uninitialized private variables.

Regarding their initial position, try using the int version of the Random.Range (by removing the "f" from the $$anonymous$$ and max)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ramlock · Aug 30, 2018 at 10:09 AM

The blank spaces are because you're positioning the tiles randomly, which I assumed was intended. If you want every tile in the grid to be filled, you should do a double loop instead of random. Try this code:

 void Start()
 {
     for(int x = 0; x < 64; x++)
     {
         for(int y = 0; y < 64; y++)
         {
             referenceTiles = Instantiate(tiles[Random.Range(0, centerPointVariety)]) as GameObject;
             referenceTiles.transform.parent = transform;
             referenceTiles.transform.localPosition = new Vector3(x * 0.64f, y * 0.64f, 0);
         }
     }
 }

Comment
Add comment · Show 2 · 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
avatar image Bossquake1 · Aug 31, 2018 at 01:07 AM 0
Share

public class mapGeneration : $$anonymous$$onoBehaviour { public int x_length; public int y_length;

 public int maxTileRange;

 private int noOfTiles;
 public int maxTiles;

 public List<GameObject> tiles = new List<GameObject>();

 GameObject referenceTiles = GameObject.FindWithTag("tileTag");

 void Start()
 {
     for (int x = 0; x < 64; x++)
     {
         for (int y = 0; y < 64; y++)
         {
             if (noOfTiles < maxTiles)
             {
                 referenceTiles = Instantiate(tiles[Random.Range(0, maxTileRange)]) as GameObject;
                 referenceTiles.transform.parent = transform;
                 referenceTiles.transform.localPosition = new Vector3(x * 0.64f, y * 0.64f, 0);
                 noOfTiles += 1;
             }
         }
     }
 }

}

this is what i have come up with, it does almost what i want. the only thing is it spawns the tiles in a straight line. i want them spawned randomly within the boundary so that i can use them as parent blocks and procedurally generate more terrain around them based on nearby tile types. Sorry if im confusing, trying my best to explain.

avatar image Ramlock Bossquake1 · Aug 31, 2018 at 01:45 PM 0
Share

Can you provide a visual aid of what you're trying to achieve?

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

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

Related Questions

Quaternion.identity problem at random spawn 1 Answer

Spawn game object in random position on screen 1 Answer

Spawn a gameobject in random position within an area? 2 Answers

Randomly Place Cubes In Viewport Without Overlap 1 Answer

Generate random number and set a GameObject active. 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