- Home /
 
De-attach child from parent and remove from animation
Hi All!
I have a GameObject with some child objects and a animator controller for parent object, in animator I move childs by curves and on keypress I need to deattach 1 child object from parent and this child should not be used in animation any more.
I do transform.parent = null for child and there are two cases:
if child do not have RigidBody2D it appears at coordinate center and update position by animation in keypress
if child have RigidBody2D it appears at coordinate center and still move by animator
so how to fully deattach child and remove them from animator process? and leave deattached child on old world position?
P.S child should be with rigidbody2d all time
version 4.5.0f6
thanks
I think that i find answer, use a Animator.Rebind
Hi,
I have exactly the same situation:
Child objects are animated my the Animator at the parent. At some point an event triggers detaching one of the child object (with child.transform.parent = null) which continues to fly across the scene. Now, the only problem is, that it seems that the child is still affected by the animator at the parent..
How did you remove the detached child from animating, while rest of the gameObjects are animated?
Thanks.
Ok, I get it now.
Just after setting child's parent to null, use animator.Rebind(), so that the animation is "refreshed" and the detached child is no longer part of the animation :)
yes, animator.Rebind() helps, only one bad thing that animation stars from beginning after it.
Answer by modegames · Jul 27, 2015 at 04:42 AM
I found this function we use helps to resolve this issue
 public static void RemoveObjectFromAnimator(GameObject gameObject, Animator animator)
         {
             Transform parentTransform = gameObject.transform.parent;
             
             gameObject.transform.parent = null;
 
             float playbackTime = animator.playbackTime;
             
             animator.Rebind ();
             
             animator.playbackTime = playbackTime;
 
             gameObject.transform.parent = parentTransform;
         }
 
              Your answer