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.
[35051-screen+shot+2014-11-11+at+17.37.10.png|35051]
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!