Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ckannon · Jul 10, 2010 at 09:37 PM · rigidbodycolliderparentchild

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spinaljack · Jul 10, 2010 at 10:45 PM 0
Share

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

avatar image qJake · Jul 11, 2010 at 06:32 AM 0
Share

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?

avatar image ckannon · Jul 11, 2010 at 03:59 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3dsquad · Nov 12, 2011 at 06:06 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges