Reputation: 65
I am trying to attach a GameObject B to another GameObject A after they collide.
Box Colliders and Rigidbodies are attached to both Objects. Following code enables the Object Merging:
private void OnCollisionEnter(Collision collision)
{
collision.gameObject.transform.SetParent(transform,true);
}
Otherwise no other script affects the objects
The Object B will be parented to Object A as expected. But right after the collision the Mesh Renderer and the Box Collider are no longer on top of each other. In order to obtain a better idea of the situation I have included pictures before the collision, while the collision and after the collision.
I also noticed that a lower collision force also leads to a lower shift. Do you have any idea how the Mesh Renderer and Box Collider can still lie on top of each other, after the collision?
Thank you!
Upvotes: 1
Views: 655
Reputation: 2586
When you parent object B to object A, its local transform gets multiplied by A's translation/rotation/scale. Solution make an object C which is parent of object A that has rotation 0,0,0 and scale 1,1,1, when object A collides with object B, make C a parent of B.
ABC Always Be Colliding
Upvotes: 1