Search Unity

Fix for broken collider when parenting to rigidbody

Discussion in 'Scripting' started by Statement, May 29, 2009.

  1. Statement

    Statement

    Guest

    Hello World!

    Today I ran into a problem when trying to clone prefabs (ship parts) and parent them to a space ship. The problem was the physics broke, resulting in all clones colliders got stuck where they were instansiated, instead of the standard behavior of them following the object.

    After a quick discussion on IRC, we managed to find a solution. Just before you add the clone to the parent through clone.transform.parent = ship.transform;, you temporarily disable the clone. After the transform binding is done, you can activate it again. This worked for me, if you are having trouble please make a reply about it.

    Here is working code from my project that creates 10 rockets to a ship.

    This script is intended to be placed on an empty game object that simply creates a few rockets on a ship when you start the game.

    Variables:

    ship - Any game object that acts as your base object to attach stuff to. It should have a rigid body and a collider.
    rocketPrefab - Any prefab you want to instansiate around ship. It should not have any rigid body, but it should have a collider.

    Note the scales on the transforms; new prefabs are placed 1 unit away from the ship. This will cause problems if your parts or ship is too large because of overlapping.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShipBuilderTest : MonoBehaviour
    5. {
    6.     public GameObject ship;
    7.     public GameObject rocketPrefab;
    8.    
    9.     void Start()
    10.     {
    11.         float piDouble = Mathf.PI * 2;
    12.         float step = piDouble / 10; // Create 10 objects
    13.  
    14.         for (float angle = 0; angle < piDouble; angle += step)
    15.         {
    16.             float cos = Mathf.Cos(angle);
    17.             float sin = Mathf.Sin(angle);
    18.  
    19.             Vector3 offset = new Vector3(cos, sin);
    20.             Vector3 position = Vector3.zero;
    21.             Quaternion rotation = Quaternion.identity;
    22.  
    23.             GameObject clone = Instantiate(rocketPrefab, position, rotation) as GameObject;
    24.             clone.active = false; // WORKAROUND FIX
    25.             clone.transform.parent = ship.transform;
    26.             clone.transform.localPosition = position + offset;
    27.             clone.transform.localRotation = rotation;
    28.             clone.active = true; // WORKAROUND FIX
    29.         }
    30.     }
    31. }
    32.  
    I hope that this will help other people with the same problem. I have sent a problem report to the developers so that it will be addressed in the future.

    Note: This works for me in Version 2.5.0f5 (21627).
     
  2. kaedmon

    kaedmon

    Joined:
    Jan 28, 2010
    Posts:
    25
    Thanks for posting your discovery!