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 Caldas · Jul 28, 2017 at 10:10 PM · random.rangeintantiate

How can i ramdomly intantiate gameObjects without them instantiating on top of each other?

In my code i create ramdom positions and instantiate the GameObject in that position, them i make a loop to intantiate more. The problem is that sometimes the GameObects are instantiated on top of each other. what can i do to solve that? Thanks.

Here is the code

 public GameObject cube;

 public Collider plane;

 int size;

 int nCubes;

 void Start()
 {
     plane = GetComponent<Collider>();

     Vector3 center = plane.bounds.center;
     Vector3 inicio = plane.bounds.min;
     Vector3 fim = plane.bounds.max;

     nCubes = Random.Range(3, 6);


     for (int i = 0; i < nCubes; i++)
     {
         Vector3 position = new Vector3(Random.Range(inicio.x, center.x), 0, Random.Range(inicio.z, fim.z));
         Instantiate(cube, position, Quaternion.identity);
     }

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

Answer by moltow · Jul 28, 2017 at 10:57 PM

You could keep a List(or array or whatever you prefer) of positions. Then, when you instantiate your GameObject add it's position to the List. Then, when creating a new position, check it against the List to make sure it isn't already used.

Comment
Add comment · Show 7 · 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 Caldas · Jul 29, 2017 at 12:18 AM 0
Share

So the position of the gameobjects will be random between the positions that i put in the array?

avatar image moltow · Jul 29, 2017 at 12:24 AM 0
Share

Sorry if I wasn't clear. The List is to keep track of Vector3 positions already in use, so when you create a new Vector3 position, you check to make sure that position doesn't already exist in the List. If it doesn't, you're free to instantiate your GameObject and then add its Vector3 position to the List. If it is the same, don't use it and generate a new Vector3 using your Random.Range technique above.

avatar image Caldas · Jul 29, 2017 at 12:31 AM 0
Share

Now i get it, thanks :)

avatar image Caldas · Jul 29, 2017 at 12:35 AM 0
Share

One more thing. If im spawning a tree, it have some size, and if its spawning its vector(2,0,1), and theres another tree at position vector(3,0,1), the will be kind like inside of each other. How should i solve it?

avatar image moltow Caldas · Jul 29, 2017 at 12:58 AM 0
Share

Ah yes. I see the problem. There are a number of ways you could approach this. I'm not sure I'm offering the best solution from a performance perspective, but here is one idea...

You could use the Bounds class. Ins$$anonymous$$d of having your List contain Vector3s, have it contain your Trees. From there, you can get to the Tree's Renderer component and from it, you can get at its bounds property. Then, when you create a new Vector3, you could also create a temporary Bounds instance, set it to use the newly created Vector3 as its center and then set its size to the size of the bounds of your tree. Then you can check to see if the bounds of any tree in your List intersects with the bounds you just created.

Does that make sense?

avatar image Caldas · Jul 29, 2017 at 01:14 AM 0
Share

Yes thanks! Im gonna try it.

avatar image a161803398874 Caldas · Jul 29, 2017 at 05:58 AM 0
Share

make a list with the spawn points and a bool variable and while a gameobject exist in that position turn the variable on or off so you cant instanciate more objects there, if youre position are predefined you will have no trouble of having one over the other

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

70 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

Related Questions

Instantiate randomly from array 1 Answer

Random.Range Spawner 1 Answer

LocalEulerAngles with random rotation doesn't work 1 Answer

Random.Range returns number in different range. 0 Answers

Random.Range always returns min value (didn't set seed) 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