Search Unity

Resolved Issue when parenting target transforms

Discussion in 'Animation Rigging' started by calpolican, May 3, 2023.

  1. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    For my rig I'm using targets to wich the head and hands will point/aim for.
    During runtime I exepect to be able to change the target parent, so that I can, say, parent it to an enemie's head and my character head will look towards it, regardless of the enemies movements.

    But I'm having an issue with where the targets are located at startup.
    1) If the target is inside the rig and I reparent them, this introduces some strange offset and my character looks at some other direction than my target's.
    2) If the target is inside the character but outside the rig, then a nasty error appears and there's some garbage allocation that forces me to reset the engine.
    3) If the target is in my scene outside my character, everything works as expected. BUT I don't like this solution, as I have to instantiate most of my characters at runtime, it would be best if I can set them in a prefab.

    As stated, the only option working is the last, but I need either #1 or #2. Does anybody know what's the propper way to do it?
     
    Last edited: May 4, 2023
  2. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    For anyone that may have the same issue, you can solve it by using a MultiParent constrain in the target. This way you don't need to reparent it. You just put it inside the rig, and set the parent via this constrain. This is some idea of how it could be used:

    Code (CSharp):
    1.     public void ChangeParent(Transform newParent)
    2.     {
    3.         // Create a new ConstraintSource object
    4.         WeightedTransform source = new WeightedTransform(newParent, 1);
    5.  
    6.         WeightedTransformArray a = targetConstrain.data.sourceObjects;
    7.  
    8.         //Clean every transform we're following.
    9.         while (a.Count > 0) { a.RemoveAt(0); }
    10.  
    11.         //Add the new transform to follow.
    12.         a.Add(source);
    13.  
    14.         //Asign the new list
    15.         targetConstrain.data.sourceObjects = a;
    16.  
    17.         //You need to rebuild the rigBuilder to update the graph.
    18.         xadre.rigBuilder.Build();
    19.  
    20.     }
    It seem to work as though as you'd set the transform position in update for every frame, but a bit more efficient.
     
    Last edited: May 5, 2023