- Home /
Encapsulate.Bounds is no longer working.
Hey guys! I'm back :P I am having a BIG issue which is halting my development phase severely!
#pragma strict
var boundsForwadPoint : Vector3;
function Update()
{
EncapsulateAll();
}
function EncapsulateAll()
{
var combinedBounds = new Bounds();
var renderers = GetComponentsInChildren(typeof(Renderer));
for (var renderer : Renderer in renderers)
{
if (renderer != this.renderer)
combinedBounds.Encapsulate(renderer.bounds);
}
boundsForwadPoint = combinedBounds.center + new Vector3(0, 0, combinedBounds.max.z);
}
function OnDrawGizmos()
{
Gizmos.DrawSphere(boundsForwadPoint, 0.25);
}
This is my code, for some reason it has stopped changing the center point of my gameObject. What this script is supposed to do, (as you can probably tell :P) is constantly update the middle point of a parent gameobject so the pivot point continuously changes if the gameobject is altered.
For example:
I am using this for a ship building feature in one of my games. Each ship is constructed by combining a group of cubes onto a parent.
Each cube which is added is supposed to change the bounds.center of the entire ship, so the parent object is constantly in the middle of the creation instead of at the parents center location.
Now for as long as I can remember, it has always worked, even the gizmo is being drawn properly, but the gameobject which is the parent isn't being moved.
//
Is there anything you guys can see that i am missing? :( Or am i on the totally wrong track here.
//
I hope this is all the information you guys need. Any help would be appreciated.
Thank you for reading, Sincerely,
Bakos133
Anyone yet? :P Really could use help on this, i think i've pretty much explored all of my options :(
Why would anything "move"? You have no code here that would do that. Also, check out Aggregate; it's fun.
combinedBounds = GetComponentsInChildren<Renderer>().Select(r => r.bounds).Aggregate((b1, b2) => b1.Encapsulate(b2));
Hey guys! Thanks again for your answers!! You've all been great! To better explain what my problem is though, i'm going to have to go into visuals.
I have 2 scripts,
A) buildScript
B) centerObject
The buildScript is used to place new blocks onto the parent object which contains the centerObject script. When the player presses B, he is presented with a single option: "Build Ship"
When the player clicks on this button, a cube and parent object are created at the players position. The player can then add blocks however he/she sees fit by just left clicking on this object.
Now what this script used to do, as seen in the illustration above, is move the parent object's(NOT THE CUBE's) center point to the exact center of the entire creation.
If 3 blocks across, the center would be the middle block, if 2 across, the center would be between the 2 blocks, if 4 blocks the center would be between the 2nd and 3rd block, and etc in a 3 dimensional space. (X, Y, Z)
In the game view you would only see the 3 blocks, yet in the editor view you would see that red, green, and yellow arrow(aka center point) move around to the exact center of the entire renderer bounds.
//
What i am trying to achieve here is the same effect this centerObject script had before, so i can move onto more pressing matters but as of this moment it is doing bunk.
I would really like to thank you both though for your assistance, but if there is any way to do this, please feel free to help/direct me towards sources i can read through to achieve what i am ai$$anonymous$$g for. I do not need the code written for me, but if you have the time and wouldn't $$anonymous$$d stepping me through the actions you took to solve the problem at hand, PLEASE!!! It would be greatly appreciated :)
//
Thank you once more!! Sincerely,
Bakos133
//
// ps. Heres another screen of whats happening ingame, code on left hand monitor and issue on right.
(As you can see in the editor view, the center point is supposed to be in the middle of the 2nd block, but it is in the middle of the initially created object.(aka. the parent))
Another solution i would love to have is how to grow the bounds of my parentShip's BoxCollider to encompass my entire creation, that wouldn't be too bad either!!
Anything helps! Please please please!!
Sorry for being so persistent guys! I have looked everywhere for a solution though and nothing has come up as of yet :(
Umm let me get this straight... You are adding children objects to the parent, and you want them to always stay centered on the parent position?
So if you had 2 objects, and you aligned your camera to view the seam between them, adding a 3rd would make you be pointing at the center of the center object??
If so this is quite easy to solve. $$anonymous$$eep the parent in the same position. Whenever you add a child, update all children's placements. Assu$$anonymous$$g they're the same width, the following pseudocode should help:
halfWidth = childWidth * children.length * 0.5f;
for (int i=0; i<children.length; ++i)
child.x = childWidth * i - halfWidth
If they have varying sizes, I can write you up a pseudocode to align them all with touching edges too. Just doesn't look like that's the case.
Also fyi, make sure your editor is set to Pivot. Looks like it is, but just making sure.
I'm fairly certain I'm misunderstanding, so please correct me if I'm off. But hopefully I got lucky and helped :)
NEVER$$anonymous$$IND! Wow i was dumb xD Thank you for the answer!!
You are amazing!! Have yourself a great day!
If you have s$$anonymous$$m, PLEASE PLEASE PLEASE add me! (very random i know, but you seem like the kind of guy who knows his stuff and i wouldn't $$anonymous$$d having you as a friend!)
risinangel <-- username (Look for GenBakos)
Once again though! Thank you so much!!! You are a god!
Sincerely, Bakos
Answer by Bunny83 · Jan 06, 2014 at 07:07 PM
I see two major problems here:
First you initialize your combinedBounds with an empty "bounds object". That means it's centered at the scene origin and has no size. When you encapsulate other bounds that aren't around 0,0,0 the resulting bounds object wouldn't be correct.
Second you use the bounds center and add the bounds max z-value, that doesn't make much sense. You probably want to add Bounds.extents.z to get the end position of the bounds volume.
The bounding box is defined in world space. You should initialize the combinedBounds either with the objects position or, which would be better, with one of the child bounds.
Something like this:
function EncapsulateAll()
{
var renderers = GetComponentsInChildren(typeof(Renderer));
if (renderers.Length == 0)
return;
var combinedBounds = renderers[0].bounds;
for (var renderer : Renderer in renderers)
{
if (renderer != this.renderer)
combinedBounds.Encapsulate(renderer.bounds);
}
boundsForwadPoint = combinedBounds.center + new Vector3(0, 0, combinedBounds.extents.z);
}
That empty bounds mistake is an easy one to make if you arent thinking about it.
I just had a similar bug, thanks for the answer!