Stick object to other on collision ?

Hello,

I’m trying to stick an object to another on collision.
I tried several solutions i found here, but none is working for me.

If i use :

transform.parent = otherObject.transform;

children doesn’t move when the parent moves

If I use :

void OnCollisionEnter(Collision hit)
{
	foreach (ContactPoint contact in hit.contacts)      
	{
        FixedJoint fixedJoint = gameObject.AddComponent<FixedJoint>();
        fixedJoint.anchor = contact.point;
        fixedJoint.connectedBody = hit.rigidbody;
	}
}

When children collide with the parent, it’s ejected in the air

transform.parent = otherObject.transform;
should definitely work. there must be some something else moving “otherObject”. maybe another script that needs disabling on contact.

maybe the child still has its own rigidbody component moving it independently after parenting?

try disabling the child’s rigidbody on contact.

or better yet, lock the rigidbody x y z on contact, and disable the child’s gravity.

you only want it to do these things with the new parent.

Problem solved, thanks for your help

Rigidbody rBody = GetComponent<Rigidbody>();

void EnableRagdoll()
{
    rBody.isKinematic = false;
    rBody.detectCollisions = true;
}
void DisableRagdoll()
{
    rBody.isKinematic = true;
    rBody.detectCollisions = false;
}

I’m a newbie and I have this code:

public class ObstaclePush : MonoBehaviour
{
    [SerializeField]
    private float forceMagnitude;
 
private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Rigidbody rigidbody = hit.collider.attachedRigidbody;
 
        if (rigidbody != null)
        {
            Vector3 forceDirection = hit.gameObject.transform.position - transform.position;
            forceDirection.y = 0;
            forceDirection.Normalize();
 
            rigidbody.AddForceAtPosition(forceDirection * forceMagnitude, transform.position, ForceMode.Impulse);
        }
    }
}

I want to attach the object1 to object2 when object1 collides with object2 so the object2 stays firm during pushing. Where should I put this two codes above into my code. Could you please show it with a clear explaination?