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 carlqwe · Apr 25, 2016 at 02:08 PM · collisioncolliderrandom

Prevent randomGenerated Structures from colliding / clipping

hello i have a random generation script that random generates all the structures and the world. But sometimes the structures collide with each other, (spawning inside of each other). How do i prevent this? Thanks

here is one of my random generation scripts:

 using UnityEngine;
 using System.Collections;
 
 public class GenerateTrees2 : MonoBehaviour
 {
     //HouseDetails
     public GameObject Tree;
     public int z;
     public int x;
     public int SpawnTimer = 30;
     public int rotationY;
     public int rotationZ;
     public Quaternion randomRotation;
     
     void Start ()
     {
         while (SpawnTimer > 0)
         {
             //RandomRotation
             int rotationZ = Random.Range(-500, 500);
             int rotationY = Random.Range(-500, 500);
             int rotationX = 0;
             
             //RandomPosition
             int z = Random.Range (-75, 75);
             int x = Random.Range (-75, 75);
             int y = -2;
             
             Vector3 housePosHere = new Vector3 (x, y, z);
             
             //Rotation
             randomRotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
             
             //LastFixes
             SpawnTimer--;
             Instantiate (Tree, housePosHere, randomRotation);
         }
     }
 }
 
Comment
Add comment · Show 2
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 Cherno · Apr 25, 2016 at 07:25 PM 0
Share

Combining the Renderer's or Collider's bounds with a Physics.BoxCast would be a simple solution.

avatar image carlqwe Cherno · Apr 26, 2016 at 09:19 AM 0
Share

Can u explain more? :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by M-Hanssen · Apr 25, 2016 at 02:53 PM

Do something like this:

 using UnityEngine;
 using System.Linq;
 
 public class GenerateTrees2 : MonoBehaviour
 {
     //HouseDetails
     public GameObject Tree;
     public int z;
     public int x;
     public int SpawnTimer = 30;
     public int rotationY;
     public int rotationZ;
     public Quaternion randomRotation;
 
     // Change this value to the desired detection radius
     public float DetectionRadius = 1;
 
     protected void Start()
     {
         // You do not want a while loop like this in your main thread, please make use of coroutines, as for the recursvie method beneath!
         while (SpawnTimer > 0)
         {
             SpawnRandomTree();
         }
     }
 
     protected void SpawnRandomTree()
     {
         //RandomPosition
         int z = Random.Range(-75, 75);
         int x = Random.Range(-75, 75);
         int y = -2;
         Vector3 housePosHere = new Vector3(x, y, z);
 
         //Rotation
         randomRotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
 
         //LastFixes
         SpawnTimer--;
 
         // Make sure there is s "TreeScript" script attached to each Tree ;-)
         RaycastHit[] hits = Physics.SphereCastAll(housePosHere, DetectionRadius, transform.up);
         if (hits.FirstOrDefault(t => t.collider.GetComponent<TreeScript>()) == null)
         {
             Instantiate(Tree, housePosHere, randomRotation);
         }
         else
         {
             SpawnRandomTree();
         }
     }
 }
Comment
Add comment · Show 3 · 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 carlqwe · Apr 25, 2016 at 04:57 PM 0
Share

Tho, the Tree script what is that?

avatar image b1gry4n carlqwe · Apr 25, 2016 at 05:39 PM 0
Share

replace the tree script with a script you attach to your houses. It looks like hes using it to spawn trees that dont overlap.

avatar image carlqwe b1gry4n · Apr 26, 2016 at 09:12 AM 0
Share

DOnt really understand..

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

60 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

Related Questions

FPS Character keeps randomly sliding and rotating 2 Answers

Collision Detection is too slow 0 Answers

Make sure objects are not instantiated on each other 2 Answers

edit a variable as a result of a collision 0 Answers

How to use contact.point from 2 seperate colliders in a collision? 0 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