Search Unity

"CopyTransformsRecurse" Best Method?

Discussion in 'Scripting' started by MadeThisName, Mar 1, 2016.

  1. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Just a Quicky for a more knowledgeable person. Is the script below the best possible way to catch the current Transforms of Said GameObject and apply them to an instantiated Ragdoll while applying Addforce.

    Code (CSharp):
    1. public void CopyTransformsRecurse (Transform src, GameObject dst)
    2.         {
    3.             dst.transform.position = src.position;
    4.             dst.transform.rotation = src.rotation;
    5.  
    6.             if (dst.GetComponent<Rigidbody> ())
    7.                 dst.GetComponent<Rigidbody> ().AddForce (hitDirection * force, ForceMode.VelocityChange);
    8.          
    9.             foreach (Transform child in dst.transform) {
    10.                 var curSrc = src.Find (child.name);
    11.                 if (curSrc) {
    12.                     CopyTransformsRecurse (curSrc, child.gameObject);
    13.                 }
    14.             }
    15.         }