How to lift the runtime child object with rigidbody?

Hi,

I have a trouble for do this one.
Actually i have attached one sphere to my mouse cursor which is having the main script.
If i pressed the mouse button when sphere collide to the other object(cube) that object be a child of my sphere and that object should move where my sphere go(because cube is a child of the sphere) till now i did.

But my when i uptake my sphere while the cube child my sphere is moving upwards but the cube is not uptake because of the gravity force. But i need to take up the cube up with the rigidbody.

Is there any other way to do this? Please help me.

Thanks in Advance.

Instead of parenting, add a FixedJoint component to the other object and set its connectedRigidbody to the sphere - this will “glue” the two objects (both must be rigidbodies!).

Supposing that you’re attaching the objects at OnCollisionEnter, the code (sphere script) could be something like this:

function OnCollisionEnter(col: Collision){
  // if mouse button pressed and the other object is a rigidbody...
  if (Input.GetMouseButton(0) && col.rigidbody){
    var fJoint = col.gameObject.AddComponent(FixedJoint);
    // connect to this rigidbody:
    fJoint.connectedRigidbody = rigidbody; 
    // make the joint unbreakable:
    fJoint.breakForce = Mathf.Infinity; 
    fJoint.breakTorque = Mathf.Infinity;
  }
}