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 JekasG · Nov 06, 2014 at 10:25 AM · c#instantiateprefabarrayloop

OverlapSphere for parallel arrays

How can i perform OverlapSphere for parallel arrays which detect when there is a overlapping of prefab gameObjects ? Here is my code which generates these prefabs in random positions and sizes.

 void Platform_Position_Scale_Generator(int i) {
 
     posX[i] = Random.Range(minPosRange, maxPosRange + 1);
     posY[i] = Random.Range(minPosRange, maxPosRange + 1);
     posZ[i] = 0;
 
     scaleX[i] = Random.Range(minScaleRange, maxScaleRange + 1);
     scaleY[i] = 1;
     scaleZ[i] = 1;
 
 }
 
 void Platform_Generator(int i) {
 
     platformPrefabPosition[i].x = posX[i];
     platformPrefabPosition[i].y = posY[i];
     platformPrefabPosition[i].z = posZ[i];
 
     Instantiate(platformPrefab, platformPrefabPosition[i], Quaternion.identity);
     platformPrefab.transform.localScale = new Vector3(scaleX[i], 1, 1);
 
 
 }
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 · Nov 06, 2014 at 01:26 PM 0
Share

What exactly are you asking? Do you want to check if the platforms you've just instantiated are overlapping any of the other platforms?

If the platforms are box shaped, it'll be really easy to check if they're intersecting each other by checking if their bounds are intersecting.

avatar image JekasG · Nov 06, 2014 at 02:50 PM 0
Share

Yes they are box shaped colliders. The problem is i don't know how to check if there are overlapping. I need help in that

avatar image Baste · Nov 06, 2014 at 02:55 PM 0
Share

About how many are you making? That'll influence the answer quite a bit.

avatar image JekasG · Nov 06, 2014 at 02:56 PM 0
Share

How does it ? For now i am making ten platforms. But maybe later if it works. It might vary to how much i will instantiate.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Nov 06, 2014 at 03:52 PM

Okay, so using an OverlapSphere to solve your problem is probably not going to work. This is simply because the OverlapSphere is a sphere, while your platforms are (probably rectangular) boxes. So if you have two long platforms that's just over each other, a sphere will detect them as "collided", even though they're not.

So, as long as you don't have very many platforms, the easiest thing will be to check each new platform against every other, to see if their bounds overlap. This will get very slow very fast if you're trying to do it with many platforms, though.

So something like:

 //field variable, remember 'using System.Collections.Generic;'
 List<Collider> platformColliders = new List<Collider>();

 void Platform_Generator(int i) {
     platformPrefabPosition[i].x = posX[i];
     platformPrefabPosition[i].y = posY[i];
     platformPrefabPosition[i].z = posZ[i];

     GameObject newPlatform = Instantiate(platformPrefab, platformPrefabPosition[i], Quaternion.identity) as GameObject;
     //fixed a bug here, don't change the scale of the prefab, change the instantiated object instead
     newPlatform.transform.localScale = new Vector3(scaleX[i], 1, 1);

     Collider newPlatformCollider = newPlatform.collider;
     bool overlaps = false;
     foreach (Collider c in platformColliders) {
         if (c.bounds.Intersects(newPlatformCollider.bounds)) {
             overlaps = true;
         }
     }
     if (overlaps) {
         //You've detected overlapping! Congratulations!
     }
     else {
         platformColliders.Add(newPlatformCollider);
     }
 }

So if you want to remove overlapping platforms, you just do

 Destroy(newPlatform);

under if(overlaps). Now, as I said, this will be fine with 10-20 or even maybe more platforms, but since you're checking the bounds of every platform against the bounds of every other platform, for 100 platforms that'll get you in the vicinity of 10.000 checks, and you'll probably need to look for something else.

Comment
Add comment · Show 1 · 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 JekasG · Nov 06, 2014 at 09:56 PM 0
Share

Oh thanks man. Thats exactly what i wanted. Im just wondering, how do i code it so when overlaps is true it instantiates another one. Why can't i just do i--

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

Prefabs instantiated from an array are keeping their public int value 1 Answer

Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer

How to deal with for loop and array? 0 Answers

How to compare and detect overlapping gameObject Prefabs using maths 0 Answers

Instantiate prefab once help C# 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