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 GabboUnity · Sep 26, 2014 at 03:57 PM · randomcolorplanerandom.rangeoverlap

Random objects don't overlap

Hi guys! I wrote a script that create random planes at random positions colored randommly. But, some of these planes overlap each other and I don't want it. I would write a script that create random planes in random position but that don't overlap them each other. How can I do? Here's the script that I attached to my MainCamera.

 #pragma strict
 
 var s : GameObject;
 var colors = [Color.red, Color.green, Color.blue];
 
 function Start () {
     StartCoroutine(creazione(1));   
 }
 function Update () {
 }
     
 function creazione(tempo) {
   
      while(true)
      {
      
        s = GameObject.CreatePrimitive(PrimitiveType.Plane);
        s.renderer.material.color = colors[Random.Range(0, colors.Length)];
        s.transform.position.y = 0;
        s.transform.position.x = 0;
        s.transform.position.z = Random.Range(0, 100);
        yield WaitForSeconds(tempo);
     }   
 }
    
Comment
Add comment · Show 4
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 Baste · Sep 26, 2014 at 09:17 PM 1
Share

You could spend a ton of time doing the maths to make the planes not overlap. Or you could give them colliders, check if they're collided when you spawn them, and then despawn them if they're collided.

I'd go with the second one. Just grab a cap on how many times the code tries to spawn stuff, so you don't get into infinite loop land.

avatar image GabboUnity · Sep 26, 2014 at 10:43 PM 0
Share

Ok, I thought the same thing, but how can I write it in my javascript file that I wrote up in the question?

avatar image robertbu · Sep 26, 2014 at 11:12 PM 0
Share

Overlap on the 'z', or overlap visually? Given that a Plane is 10x10, at most you can fit 11 planes using positions between 0 and 100 on the 'z'. Will your planes remain with localScale of (1,1,1)?

avatar image GabboUnity · Sep 26, 2014 at 11:23 PM 0
Share

Overlap on Z axis and visually you can look only colored stripes of random planes that appear on the other planes that are created before. It happens because the planes doesn't stop creating, they are random, so they continue to appear in the scene with other random colors.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Sep 27, 2014 at 12:10 AM

I hesitate to answer since I'm fuzzy on your criteria. But here is a script. It works by keeping a list of the Transforms of the objects created. When a new position is selected, it tests that position against the existing positions. Since a plane is 10 x 10 units, if a position is less than 10 units from an existing position, then there will be an overlap. It tries 50 times to find a position that fits. If it fails, it bails out of the coroutine.

 #pragma strict
 
 import System.Collections.Generic;
 
 var list : List.<Transform> = new List.<Transform>();
 
 var s : GameObject; 
 var colors = [Color.red, Color.green, Color.blue];
 
 function Start () { StartCoroutine(creazione(1)); }
 
 function creazione(tempo) {
 
      while(true) {
          var pos : Vector3;
          for (var i = 0; i < 50; i++) {
              pos = Vector3(0f,0f,Random.Range(0,100));
              if (!CheckOverlap(pos)) break;
          }
          if (CheckOverlap(pos)) break;
          
         s = GameObject.CreatePrimitive(PrimitiveType.Plane);
         s.renderer.material.color = colors[Random.Range(0, colors.Length)];
         s.transform.position = pos;
         list.Add(s.transform);
         yield WaitForSeconds(tempo);
     }
 }
     
 function CheckOverlap(pos : Vector3) {
     for (var t : Transform in list) {
         if (Vector3.Distance(t.position, pos) < 10.0)
             return true;
     }
     return false;
 }   

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 GabboUnity · Sep 27, 2014 at 10:17 AM 0
Share

Well, I tried it this morning and it's perfect for my game! But now I have an other question: I have a ball that roll on these random colored planes, I also want make it colored randomly and I want to check when it rolls on the plane that has the same color. How can I do? Thanks a lot.

avatar image robertbu · Sep 27, 2014 at 01:57 PM 0
Share

@GabboUnity - You can raycast down to access the plane, or given the code above, you can use the position of the ball and find the plane in the list. But this is really a new question. We like to keep each question focused on a single issue, so you should open a new question with a complete description of your problem.

avatar image GabboUnity · Sep 27, 2014 at 02:20 PM 0
Share

Ok, in a little time I'll write this new question in another topic! Please follow my profile to see it and answer me. Thanks a lot.

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

randomly change between 6 colors C# 4 Answers

Switch between random colors 4 Answers

Random.Range(..) not working 1 Answer

How do you get different random numbers for each object array? 1 Answer

Using Vector3 in ViewportToWOrldPoint 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