Search Unity

Collision problem with dynamically created compound objects

Discussion in 'Editor & General Support' started by xenotime, Apr 27, 2010.

  1. xenotime

    xenotime

    Joined:
    Apr 27, 2010
    Posts:
    12
    Hello all. (I'm a french guy so I prefer apologize for my language)

    Here my current problem.
    I have two GameObject :
    GameObject A with a Rigidbody, a CapsuleCollider and a CustomJoint (with some motion and angular constraints but no Connected Body).
    GameObject B with only a CapsuleCollider.

    If I do parenting object B to object A before launching the engine, I obtain a fully correct compound object with a collider formed by the two capsules.
    But if I try to do parenting at runtime (with the Hierarchy panel or by scripting) the result compound object has a correct physical behavior (A and B moves correctly together) but the object B doesn't collide anymore with the ground (Terrain).

    Do you ever meet this type of problem and have you an idea to fix it ?

    Thanks in advance for your help.
     
  2. xenotime

    xenotime

    Joined:
    Apr 27, 2010
    Posts:
    12
    After several tests, I found a bit more informations about my problem.

    If I do parenting B to A out of runtime
    objectB.collider.attachedRigidbody correspond to the rigidbody of the A object. It's ok.

    But if I do parenting B to A at runtime
    objectB.collider.attachedRigidbody is NULL. This is abnormal.

    Someone can explain to me why, please ?
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The rigidbody doesn't recognise child colliders added to an existing object. If you destroy the existing rigidbody component and then add a new one after the new object is parented, it will incorporate both colliders correctly:-
    Code (csharp):
    1. B.parent = transform;
    2. DestroyImmediate(rigidbody);
    3. gameObject.AddComponent(Rigidbody);
     
  4. xenotime

    xenotime

    Joined:
    Apr 27, 2010
    Posts:
    12
    Thank you for your answer and your helpful tip.
    I found a mean to create the rigidbody after all the colliders and the problem is now fixed.