Collider bounds encapsulate doesn't seem to do anything
Hi, today i ran into the following problem: I need to add a (Box)Collider to a GameObject loaded from an AssetBundle. Since it is a complex geometry, i don't want to add colliders to all children of the root node, but rather just add a collider to the root node and grow it to the size of the full geometry.
From what i read so far all i would have to do is call collider.bounds.encapsulate on all the children of the root node. However when i do that, nothing at all changes about the collider, no matter what i try.
In the code below i made a simplified example of what i mean (instead of loading a geometry i just create 2 primitives) and in the comments you can see the Debug.Log result.
As you can see no matter what i try the bounds of the default boxCollider of my cube primitive stay the same.
So my question is what am i doing wrong??
public class test : MonoBehaviour
{
void Start()
{
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(0, 0, 0);
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Destroy(sphere.GetComponent<SphereCollider>());
//I intentionally remove the collider, since i want to grow
//the cubes collider to include the sphere
sphere.transform.SetParent(cube.transform);
sphere.transform.localPosition = new Vector3(2, 0, 0);
cube.GetComponent<BoxCollider>()
.bounds
.Encapsulate(sphere.GetComponent<Renderer>().bounds);
Debug.Log(cube.GetComponent<BoxCollider>().bounds);
//Result: Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
cube.GetComponent<BoxCollider>()
.bounds
.Encapsulate(new Bounds(Vector3.zero, new Vector3(100, 100, 100)));
Debug.Log(cube.GetComponent<BoxCollider>().bounds);
//Result: Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
cube.GetComponent<BoxCollider>().bounds.Expand(500);
Debug.Log(cube.GetComponent<BoxCollider>().bounds);
//Result: Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
}
}
I have a similar problem with bounds.Set$$anonymous$$in$$anonymous$$ax a.GetComponent().bounds.Set$$anonymous$$in$$anonymous$$ax(new Vector3(-1, -1, -1), new Vector3(1, 1, 1)); Debug.Log(a.GetComponent().bounds);
There isn't any change in the value of bounds... any help?
Answer by Yaeker · Jun 27, 2019 at 03:28 PM
Hi,
I know this is an old thread, but for new people coming here the solution follows the same principle as transform.position or most structs:
BoxCollider cubeCollider = cube.GetComponent<BoxCollider>();
Bounds b = cubeCollider.bounds;
b.Encapsulate(sphere.GetComponent<Renderer>().bounds);
cubeCollider.center = b.center;
cubeCollider.size = b.size;
I'm not sure if I should start a new thread or revive this old one to ask this...
How do you accomplish this for a Collider2D? Collider2D.bounds is read only and it doesn't have any public accessors for center/extents/etc. that I can find. Is it possible to change the size of a Collider2D at runtime?
EDIT: Never$$anonymous$$d, I figured it out. See here: http://answers.unity.com/answers/1765662/view.html
Leaving this comment in case someone finds it useful in the future.