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 RPG-Leon · Dec 08, 2019 at 02:33 PM · listexceptionrandom gen

Making a Script that places Objects at random possition but not at the same as a taken position

I want to make a script for a game that places a set of Objects at random positions in a grid. The spawningprocess and grid are already set up but my problem is that sometimes objects spawn at the same possition. I roll a random X and a random Z position (Y is always 0), round that to conform with the grid and then create the Object at the end position. Is there any way to prevent the objects from spawning at the same position? I would just make one of the objects respawn at a different possition if it detects that it spawned inside another object but that seems very inefficient seeing how the game could end up randomly generating the object at a taken position again and could in theory take forever to find an empty spot

In the Awake Function:

         for (int x = amountOfCoalSpawned; x < amountOfCoal; x++)
         {
             Vector3 PositionToPlace;
             PositionToPlace.x = Random.Range(grid.minX, grid.maxX);
             PositionToPlace.y = 0;
             PositionToPlace.z = Random.Range(grid.minZ, grid.maxZ);
             SpawnCoal(PositionToPlace);
             Debug.Log("Coal");
         }

SpawnCoal Function:

     void SpawnCoal(Vector3 clickPoint)
     {
         Vector3 endPosition = grid.GetNearestPointOnGrid(clickPoint);
         Debug.Log("Passing Coal Position");
         Instantiate(CoalObject, endPosition, Quaternion.identity);
 
     }

The grid.GetNearestPointOnGrid function simply takes the x and z values and rounds them to full intertures and passes them back.

Like i said, the script works but i dont know how to prevent objects spawning within eachother if i make it spawn multiple ones

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
1
Best Answer

Answer by Larry-Dietz · Dec 08, 2019 at 04:02 PM

Assuming that your "coal" has a collider attached, something like this should work.

 for (int x = amountOfCoalSpawned; x < amountOfCoal; x++)
          {
              Vector3 PositionToPlace;
              bool Placed=false;
              int Count=0;
              while(!Placed)
              {
                   if(count++>100)
                       break;
                  PositionToPlace.x = Random.Range(grid.minX, grid.maxX);
                  PositionToPlace.y = 0;
                  PositionToPlace.z = Random.Range(grid.minZ, grid.maxZ);
                  if(Physics2D.OverlapCircle(PositionToPlace, .01f == null)
                      Placed=true;
              }
              SpawnCoal(PositionToPlace);
              Debug.Log("Coal");
          }

What this is doing, is repeating your random position code until it is not able to find a collider at the position, then calling your spawn method.

The count variable is to stop an infinite loop, in the case that it can't find a place to spawn, at which point, it will go ahead and spawn over top of something. Change the 100 to however many attempts you want it to make before giving up.

I typed this directly in the answer, so no guarantees against typos :)

Hope it helps, -Larry

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

121 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 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

Why does List .ToArray throw ArgumentException? 1 Answer

Argument out of range 0 Answers

Using List.Count on Unity iPhone 2 Answers

InvalidOperationException: The list was modified. 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