x


Add sub object to Rigidbody

I have a Rigidbody on a spaceship. The spaceship has mountpoints which are parented to the main ship. During runtime I want to mount a weapon to those mountpoints, the weapon is a prefab with a box collider.

The mounting process works fine and the weapon is collidable, however it seems the rigidbody is not being set to the collider.attachedRigidBody, and the collider remains a static collider (i.e nothing exerts force on it except its parent).

So the basic effect is, if you run the weapon into something physics goes to hell and no torque or force is sent back to the parent object (the ship). Where if the ship is struck, the physics still work properly.

Is there any way to add a child collider to a rigidbody at runtime?

*Further info Here is a sample of the code:

public void MountWeapon(int wepIndex) {     
    foreach (Transform go in transform) {
        if (go.tag == "MountPointWeapon") {
            Transform shipMount = go;
            foreach (Transform mountedWep in shipMount) {
                Destroy(mountedWep.gameObject);
            }               
            GameObject weapon = Instantiate(Weapons[wepIndex], shipMount.transform.position, Quaternion.identity) as GameObject;
            DebugConsole.Log("Mounting weapon: " + weapon.name);                        
            weapon.transform.parent = shipMount;                                        
            Weapon wep = weapon.GetComponentInChildren<Weapon>();
            if (wep != null) {
                weapon.transform.localRotation = Quaternion.Euler(wep.MountRotation);                                           
            }
        }
    }
}

*Further update: Now I have found a workaround for the problem. If I parent the weapon to the shipMount and then add a bounding box to the weapon, it works fine. Like so:

public void MountWeapon(int wepIndex) {     
    foreach (Transform go in transform) {
        if (go.tag == "MountPointWeapon") {
            Transform shipMount = go;
            foreach (Transform mountedWep in shipMount) {
                Destroy(mountedWep.gameObject);
            }               
            GameObject weapon = Instantiate(Weapons[wepIndex], shipMount.transform.position, Quaternion.identity) as GameObject;
            DebugConsole.Log("Mounting weapon: " + weapon.name);                        
            weapon.transform.parent = shipMount;                            
            BoxCollider col = weapon.AddComponent<BoxCollider>();
            Vector3 size = new Vector3(3,3,25);
            col.size = size;
            Vector3 center = new Vector3(0, -2.7f, 23);
            col.center = center;
            Weapon wep = weapon.GetComponentInChildren<Weapon>();
            if (wep != null) {
                weapon.transform.localRotation = Quaternion.Euler(wep.MountRotation);                                           
            }
        }
    }
}

Thanks, Chris

more ▼

asked Jul 10 '10 at 09:37 PM

ckannon gravatar image

ckannon
1 2 2 7

Have you tried adding the weapon using a fixed joint instead of parenting? That way all physics forces on the weapon will be passed on via the joint. Also the rigidbodies of the weapons will add mass and drag of their own to the ship movements

Jul 10 '10 at 10:45 PM spinaljack

Colliders automatically set "attachedRigidBody" when they have a parent Transform, you don't need to do it manually, just make sure your object is parented to the ship. I'm not sure what else could be going wrong, so long as you parent a rigidbody to another, all forces should always impact the entire structure?

Jul 11 '10 at 06:32 AM qJake

Fixed joints still flex a little unfortunately, so that's not an option. As for the parenting of the rigidbody, that seems only true if not done via code. i.e. if I add the ship as the parent of the object via the gui, it works fine. If I do it via code (weapon.transform.parent = weaponmount.transform) it does not work (collider.attachedRigidbody remains null). Perhaps this is a bug?

Jul 11 '10 at 03:59 PM ckannon
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I also had a problem with .attachedRigidbody property not being updated after chaging parent at run-time, but just discorevered when you disactivate and activate the game object with the problematic collider - everything is as it should be in the first place!

Try this one :

weapon.transform.parent = shipMount;   

weapon.gameObject.active = false;
weapon.gameObject.active = true;
more ▼

answered Sep 01 '10 at 12:20 PM

Ziggy gravatar image

Ziggy
11 1

Thanks for sharing this piece of knowledge! I still can't vote up but I wish I could

Nov 12 '11 at 06:06 AM 3dsquad
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1780
x1682
x418
x407

asked: Jul 10 '10 at 09:37 PM

Seen: 2134 times

Last Updated: Nov 12 '11 at 06:06 AM