Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have several scripts attached to static and non static models of my scene. All models are instantiated at run-time (and must be instantiated at run-time because I'm procedural building the scene).

I'd like to add a BoxCollider or SphereCollider to my FBX models at runtime.

With non animated models it works simply requiring BoxCollider component from the script attached to my GameObject. BoxCollider is created of the right dimension. Something like:

[RequireComponent(typeof(BoxCollider))]
public class AScript: MonoBehavior
{

}

If I do the same thing with animated models, BoxCollider are created of the wrong dimension. For example if attach the script above to penelopeFBX model of the standard asset, BoxCollider is created smaller than the mesh itself.

How can I solve this?

share|improve this question

1 Answer 1

Have you tried using Bounds to generate the box collider? You use something like this in runtime:

   Bounds mapBounds = new Bounds (transform.position, Vector3.one);
      foreach(Transform child in this.transform) {
        mapBounds.Encapsulate(child.gameObject.renderer.bounds);
   }

And then Add a BoxCollider and adjust according to those bounds.

share|improve this answer
    
thanks for the suggestion. I didn't know about that. I'll try your solution as soon as possible. –  Heisenbug Oct 22 '12 at 7:47
    
sorry..It doesn't work. with rig datas inside the animated models, even with your method the size of mapBounds is wrong. It seems that rig controls point are included while calculating bounds. –  Heisenbug Oct 22 '12 at 9:02
    
I'm sorry to hear that. Have your tried asking this in unity answers? You might have better luck asking such a specific question there. –  Pedro Caetano Oct 22 '12 at 20:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.