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
1
Question by mustang4484 · Jun 10, 2019 at 08:05 AM · instantiaterandomspawndistanceminimum

Instantiate gameobject with minimum distance from another gameobject

Hi guys, I should instantiate a series of objects but always leaving a minimum distance between them otherwise they go into overlap. Any suggestion?

 public void spawn()     {
         Vector3 pos = center + new Vector3(Random.Range(-size.x , size.x),Random.Range(-15,-25) , Random.Range(size.z , size.z / 2));
        
         CuboCount = Instantiate(cuboprefab, pos, randomRotation) as GameObject;
         
 
         Destroy(CuboCount, 30f);
         CuboCount.transform.SetParent(Cubofolder.transform);
         GameObject aereo = GameObject.Find("Main");
 `
 
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

Answer by Kennai · Jun 10, 2019 at 09:41 AM

Hey, @mustang4484 ! How about to use matrix of whole numbers? Lets say, you have matrix 15 x 15 x 5. its a whole numbers. You create same matrix as an array of array of array. once you spawn your object, you can assign random position in that matrix + random position inside matrix. I mean - you get random position (3,6,2). then you add small random values to, but so that your object wont exceed matrix cell volume. it's like add "small" noise to position - maybe random of 0.2f to each value so you may have position (3.1f , 5,57f ,1,8f) then you set in your array that position (3,6,2) is busy.


when you will generate new spawn position, you will need to count, how many objects already is spawned in same row and same column and same height and decrease random by that value for eaxh axis.


About how to get random position: For better view, represent your matrix as a long long row., where 0 is free, 1 is busy position. Lets say, we have row with 10 positions: (0, 0, 0, 1, 1, 0, 1, 0, 0, 1) - it has already 4 other spawned objects of max 10 positions. Now you need to generate new random position. How to do it:

max value is 10 here

  1. you already have 4 spawned objects, so you may have max = 10-4 = 6 new positions.

  2. get random value of 6 - let it be 3 - generated value.

  3. now start your loop until generated value is below zero.

  4. check each cell, if cell is free - decrease generated value. if cell is busy, go to next cell.

  5. once generated value is zero - you got your index position. in this example it would be (0, 0, 0, 1, 1, >1<, 1, 0, 0, 1). Position marked as >1<


its a 5 index, lets say it means 5f (but in your way, you will need to convert this value into matrix position, like (x,y,z)). then you add "small" noise - random of 0.2f to each axis and done.

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

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

Spawn at random? 1 Answer

Why does inside unit circle use the world point? 2 Answers

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

Instantiate multiple objects and have them raycast 1 Answer

Spawning objects at random Vector3 3 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