- Home /
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
Have you tried adding the weapon using a fixed joint ins$$anonymous$$d 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
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?
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?
Answer by Ziggy · Sep 01, 2010 at 12:20 PM
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;
Thanks for sharing this piece of knowledge! I still can't vote up but I wish I could
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Child affecting parent rigidbody? 1 Answer
Unity creating static colliders 1 Answer
How to detect child object collisions on parent 3 Answers
Trigger BoxCollider Fails to Move With GameObject After Resize 0 Answers