So I want to have a simply cube game object with a collider on it and pass to it any random mesh I choose.
During Start I want it to instantiate the mesh I set and then change the cube (and the collider) to match the instantiated model’s size, then make the model a child of this cube.
the much simpler way would be to add a box collider to the object being instantiated as then it will have the correct size already but as it happens, the parent object has the physics code attached and the OnCollision and OnTriggerEnter code and all that fancy stuff that will not get triggered if it doesn’t have a collider so I need the parent to match the size of the child.
Since the child object will always have a scale of 1,1,1 setting the cube’s scale is of no help and when I try to use MeshCollider.bounds or Collider.Bounds it tells me that Bounds read only so all the public setters I have access to are useless to me because I am accessing it via a read only variable.
So here I am, trying to something as simple as making one box the same size as the other and I find myself clueless how to do something that sounds so simple…
Can anyone please tell me how I can create a normal cube object from the main menu and then, at runtime, set it’s size to match the bounds of another object? Right now my mesh is like 100 times smaller than the cube but has a scale of 1,1,1, so how do wrap the cube around the mesh?
Thanks
Here is what I am trying to do. The commented out line is moaning about bounds being read only and Encapsulate is useless because it is already larger than the projectile. I tried setting it to Vector3.zero first then calling it to encapsulate bounds but that just makes the child object’s scale 0,0,0 (???)
This sound be such a simple thing and yet I have no idea how to do it… Please help me…
Transform p = Instantiate<Transform>(projectile_prefab);
if (null == p)
{
Debug.LogError("Projectile not found");
return;
}
p.position = transform.position;
Bounds bounds = p.gameObject.GetComponent<Renderer>().bounds;
MeshRenderer r = GetComponent<MeshRenderer>();
// r.bounds.extents = bounds.extents;
r.bounds.Encapsulate(bounds);
p.SetParent(transform);
p.localRotation = Quaternion.identity;