Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

attaching/detaching object in runtime?

Discussion in 'Scripting' started by pretender, Jan 22, 2014.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    Hi!

    I am trying to figure out efficient way to attach and detach objects in run time. It is actually easy like this
    Code (csharp):
    1. childTransform.parent = parent.Transform;
    to
    Code (csharp):
    1. parent and childTransform.parent = null
    to unparent the object, but that is only that case if you are using pivot points of both objects.

    I have a situation where i have object with multiple attach points. So one object has its pivot point but also should have other points where child object will be attached. Also child object, if they have multiple attach points, beside their pivot point they should have more attach points.

    I started using dummy objects for this. and here is how hiearachy looks

    Code (csharp):
    1. Parent I (RigidBody)
    2.    Object's Mess (Mesh and Collider)
    3.   Connections (Empty GameObject as a Container)
    4.             AttachPoint I (Empty GameObject - only Transform)
    5.             AttachPoint II (Empty GameObject - only Transform)
    Code (csharp):
    1. Child (RigidBody)
    2.    Object's Mess (Mesh and Collider)
    3.   Connections (Empty GameObject as a Container)
    4.             AttachPoint to Parent I (Empty GameObject - only Transform)
    So when I am attaching child to parent i do it like this:

    Code (csharp):
    1. child.GetComponent<RigidBody>().IsKinematic = false; //I want kinematic RigidBody while object is attached
    2. child.transform.position = parentAttachPointI.transform.position //this sets objects position not taking child attach point into account
    3. child.rotation = parentAttachPoint.rotation;
    4. child.parent = parentAttachPoint;
    Everything works ok, except the fact that I can't figure out how to take attach points into account? The children are attached and detached just fine but on wrong position (using objects transform not dummy objects)

    If i use dummy objects, because of the nature of the hierarchy. they are unparented and parent to the other object but nothing happens since they are empty

    Does any of this makes sense?
     
  2. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    Hi!

    I managed to work something out...here is what i did

    First I parented child to connection point, then made connection point to be child of object that i want to attach to. Then connection point position and rotation are set to attachment point (object we want to attach to),Then I parented connection again to the child to make hierarchy same again.

    code is something like this:

    Code (csharp):
    1.  
    2. child.transform.position = Vector3.zero;
    3. child.transform.rotation = Quaternion.identity;
    4.  
    5. childAttachPoint.parent = null;
    6. child.parent = null;
    7. child.parent = childAttachPoint;
    8.  
    9. childAttachPoint.parent = parentAttachPoint;
    10.  
    11. //position is set
    12. childAttachPoint.position = parentAttachPoint.transform.position;
    13.  
    14. //rotation is set
    15. childAttachPoint.rotation = parentAttachPoint.transform.rotation;
    16.  
    17. //unparent child attachpoint and attach it to child again
    18. childAttachPoint.parent = null;
    19. child.parent = null;
    20. childAttachPoint.parent = child;
    21. child.parent = parentAttachPoint;
    22.  
    so everything works but now i have a problem of things going slowly
    unparenting two objects, then parenting, then unparenting and parenting again takes its toll to performance I guess it will be even worse if there is a chain of objects together.

    Is there any way to speed this thing up, use another method or i am stuck with this one?

    thanks?