- Home /
Attaching objects on collision
I have a script that on the collision of two 'parts' one is parented under the other. If I drag the parent object, both objects are moved, but if the child is dragged, only the child moves. I don't know how I can fix this, as I want both objects to remain attached no matter if the parent of child is moved. Here is my script:
using System.Collections;
public class ObjectAttach : MonoBehaviour {
void Start()
{
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag=="Part")
{
collision.transform.parent = transform;
}
}
}
How are you dragging them? Is the parent/child relationship important?
Oh, I am using the dragrigidbody script from standard assets
Seems like a misplaced question. What you describe is expected behaviour and your script looks fine. Your actual issue is nothing to do with the question title :P
I know there are no errors or problems, but I want to know how to change my script to that no matter if you drag the parent or child, they move together. I tried it with a group, but had no luck as they still moves separately.
Since the drag rigidbody script drags whatever rigidbody you grab, you'll have to make it find the topmost parent of whatever you grab to make everything move together. I'm not sure about getting the topmost one but you can use transform.parent to find whatever the transform is a child of.
Answer by wijesijp · Dec 07, 2013 at 12:14 PM
Instead of parenting what you can do is join the objects with a Joint. Then you can move any of them as they were attached.