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 entity476 · Nov 11, 2014 at 04:57 PM · instantiateboundsmeshfilterlocalscalelocalrotation

Instantiating prefabs within the MeshFilter volume of object

Hello all! I would need some help here, since it is hard due to my knowledge to conceive the whole picture of the concept/problem.

alt text

I have the above scene, where these green-outlined objects are children of another object and they are basically primitive cubes rotated and scaled. I would like to instantiate these (smaller) blue prefabs at random positions within their volumes . At run time, I find these children objects and by calling the following function (part of the code) for each one, I get their MeshFilter bounds.

 function CalcBounds(index : int, go : GameObject){
     var bounds : Bounds = go.GetComponent(MeshFilter).sharedMesh.bounds;
     var center : Vector3 = bounds.center;
     var extents : Vector3 = bounds.extents;
 
     v3Center[index] = go.transform.TransformPoint(thisCenter);
     v3Extents[index] = Vector3(extents.x * go.transform.localScale.x,
                             extents.y * go.transform.localScale.y,
                             extents.z * go.transform.localScale.z); 
 }

From these values I try to instantiate the blue prefabs at offset points around the children’s bound.center with this other code in another function:

 spawnPoint = Vector3(v3Center[j].x + Random.Range(-v3Extents[j].x, v3Extents[j].x),
                     v3Center[j].y + Random.Range(-v3Extents[j].y, v3Extents[j].y),
                     v3Center[j].z + Random.Range(-v3Extents[j].z, v3Extents[j].z));

 var blueObject : GameObject = Instantiate(prefab, spawnPoint, transform.rotation);



The result, however, is that the blue prefabs are instantiated near the locations I want, but also outside the children objects’ volume. I believe the problem has something to do with the fact that although I take into account the scaling of the children objects with *go.transform.localScale, I don’t do the same with the localRotation. Also the transform.rotation in the Instantiate function refers to the parent object of the green children, on which the script is attached. If it is so, what should I do to implement rotation and at which point of the flow?

Thank you in advance!

screen shot 2014-11-11 at 17.37.10.png (38.7 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

1 Reply

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

Answer by robertbu · Nov 11, 2014 at 05:08 PM

While you translate the center to world coordinates, the corners of the bounds will also be in local coordinates and not necessarily axes aligned. Your generation of the spawn point is assuming an axes aligned cube.

The solution is to generate the random point in local coordinates, and then convert the result to world coordinates. Assuming a built-in cube, you can do:

 localPosition = Vector3(Random.Range(-.5, .5), Random.Range(-.5, .5), Random.Range(-.5, .5));
 spawnPoint = go.transform.TransformPoint(localPosition);

Note this assure the position is inside the cube, not the whole blue cube. You'd have to tighten up the range to assure the whole blue cube is inside.

If you are going to be extending this concept to mesh objects other than cubes, you can use a Random.Range() based on the extents of the mesh.bounds rather than a hard-coded 0.5.

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 entity476 · Nov 11, 2014 at 05:16 PM 0
Share

Thank you robertbu, you're great! Your immediate answer helps me to move on. It took me some time to modify my code, because I also want to control the chances according to which a new prefab will be instantiated in one child or another, depending on the child's size, and therefore to have a uniform distribution among all the children for a given amount of prefabs. Because of my workaround, I had to take localScale into consideration as well. I'm sure I complicate my code the way I do it, but regarding my question, I understand now that I don't need the meshFilter.bounds in my case, since the children are primitive cubes and therefore the $$anonymous$$eshFilter extents will be always +-0.5 and the bounds.center always the transform.point of the child, which are the values I only need. I hope my reasoning is correct, but I need to understand more about local and world space, I guess.. Also a graphical explanation in Unity's documentation of different kind of Bounds would be rather helpful.

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

26 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

Related Questions

Instantiate within boundaries of Mesh shape 2 Answers

Issue with changing localScale of gameobject after instantiate 1 Answer

Are my bounds right world position in this case ? 1 Answer

Instantiating and setting localScale doesn't work 1 Answer

Checking if object intersects? 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