Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SamuelOakley · Dec 16, 2014 at 11:43 AM · c#prefabpositioncollidersspawning

Stop buildings from spawning inside eachother

Hi, I have a really simple game world and I am trying to spawn a range of building prefabs in a random position and rotation. They do that okay, but they will sometimes spawn inside each other and because of the rigidbodies and colliders they tend to explode. So I think I need something that checks where the prefabs are going to be spawned and makes sure that none of the colliders would cross over, but if they do try spawning the prefab in a different position.

 public GameObject[] WorldObjects;
     public int amountToSpawn;
     public Vector3 spawnValues;
 
     void Start()
     {
         GenerateWorldPrefab();
     }
 
     public void GenerateWorldPrefab()
     {
         for (int i = 0; i < amountToSpawn; i++)
         {
             Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
             Quaternion spawnRotation = new Quaternion(0, Random.Range(0, 360), 0, 90);
             int r = Random.Range(0, WorldObjects.Length);
             Instantiate(WorldObjects[r], spawnPosition, spawnRotation);
         }
     }

This is what I have so far. Any help or suggestions would be great, thanks.

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 SkaredCreations · Dec 16, 2014 at 11:46 AM

Use OverlapSphere and Instantiate only if the tag or layer or component of your "buildings" is not existing in the list returned by it, you may want to enclose the whole "for" body inside a while loop so that it'll keep trying to get a position until it can Instantiate (it would be better to have it in a coroutine so that you can have yield return null after each loop cycle to avoid CPU overhead).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject change Position after game started 1 Answer

Script uses transform.position of Prefab instead of Instance 1 Answer

Checking Position 1 Answer

Clone of rigid body prefab destruction if at certain position 3 Answers

Why can't I instantiate an object that is +5 in the x and z axis? 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