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