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 SilverHuber · Dec 04, 2017 at 02:56 PM · instantiatemeshboundsmeshfilter

Instantiate within boundaries of Mesh shape

Hi all, I want to spawn certain gameobjects, but only within the boundaries of certain meshes. Below is a picture of what a shape could look like. Tried to find it before asking, but couldn't find what I needed. I know how to spawn at the vertices, now just a way to fill the spaces between the verts. Excuse me for my mediocre English. alt text

screen-shot-2017-12-04-at-154651.png (33.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Buckslice · Dec 05, 2017 at 12:02 AM

Probably the simplest way would be to generate random points in the Collider2D.bounds of the polygon then use Collider2D.OverlapPoint to check if they are inside the actual polygon.

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

Answer by SilverHuber · Dec 05, 2017 at 11:35 AM

Thanks!! So it worked for the biggest part. The only problem is the offset in the bounds. I get different offsets depending on the scale of the spawner. Maybe the center is wrong?

 Collider2D thisCollider = GetComponent <PolygonCollider2D> ();
 Bounds bounds = thisCollider.bounds;
 
 
 
 float rangeOfX = (bounds.size.x * 0.5f);
 float rangeOfY = (bounds.size.y * 0.5f);
 
 
 Vector3 newVec = transform.TransformPoint (new Vector3 (Random.Range (-rangeOfX, rangeOfX), Random.Range (-rangeOfY, rangeOfY), spawnedObject.transform.position.z));
 
 spawnedObject.transform.position = newVec;


Below some pictures of the result: Without overlap check alt text

With overlap check alt text


screen-shot-2017-12-05-at-123029.png (115.4 kB)
screen-shot-2017-12-05-at-122938.png (180.1 kB)
Comment
Add comment · Show 5 · 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 Buckslice · Dec 05, 2017 at 07:50 PM 1
Share

Use bounds.$$anonymous$$ and bounds.max, something like this

 Collider2D thisCollider = GetComponent <PolygonCollider2D> ();
 Bounds bounds = thisCollider.bounds;
 
 for(int i = 0; i < count; ++i){
     float x = Random.Range(bounds.$$anonymous$$.x, bounds.max.x);
     float y = Random.Range(bounds.$$anonymous$$.y, bounds.max.y);
     if(col.OverlapPoint(new Vector2(x,y))){
         Vector3 spawn = new Vector3(x,y,0);
         GameObject spawnedObject = Instatiate(prefabObject,spawn,Quaternion.identity);
     }else{
         continue;
     }
 }
 
avatar image SilverHuber Buckslice · Dec 06, 2017 at 09:20 AM 0
Share

Perfect! That is exactly what I needed. One last question: is there a way to make sure every created instance is within the mesh?

    if(col.OverlapPoint(new Vector2(x,y))){
          Vector3 spawn = new Vector3(x,y,0);
          GameObject spawnedObject = Instatiate(prefabObject,spawn,Quaternion.identity);
      }else{
          //generate new spawn until the spawn is overlapped.
      }

avatar image Buckslice SilverHuber · Dec 06, 2017 at 09:53 AM 0
Share

Isn't that what we have accomplished? Everything should spawn inside the mesh, or do you mean like where the smaller objects can still spawn and hang off the edge of the main shape and how to prevent that? If so I would just downscale the main collider shape a little bit to offset it.

Show more comments

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

97 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

Related Questions

unity3d can't create instantiated reference in awake 1 Answer

Getting Bounding box from a collider with children 1 Answer

Bounds of dynamic mesh not updating 1 Answer

Instantiating prefabs within the MeshFilter volume of object 1 Answer

not working- GameObject.GetComponent(MeshFilter).mesh; 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