- Home /
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.
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!
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.
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
![](https://koobas.hobune.stream/wayback/20220613171249im_/https://answers.unity.com/themes/thub/images/avi.jpg)