Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Bone Transform reference not updating

Discussion in 'Scripting' started by carcasanchez, Nov 30, 2021.

  1. carcasanchez

    carcasanchez

    Joined:
    Jul 15, 2018
    Posts:
    177
    Hi!
    I am making an animation recorder that writes anim data to a txt (for matlab purposes). For performance reasons, instead of doing:
    Code (CSharp):
    1. for each animation frame
    2. {
    3.  animator.ForceStateNormalizedTime(time);
    4.   for each human bone
    5.   {
    6.        finalText += animator.GetBoneTransform(currentBone)
    7.    }
    8. }
    I would like to caché and reference all bone transforms into an array:

    Code (CSharp):
    1.  
    2. Transform[] allTransforms = new Transform[55];
    3. for(int i = 0; i<55; ++i)
    4. {
    5.     allTransforms[i] = animator.GetBoneTransform((HumanBodyBones)i);
    6. }
    7.  
    8. for each animation frame
    9. {
    10.  animator.ForceStateNormalizedTime(time);
    11.   for each Transform t in allTransforms
    12.   {
    13.        finalText += allTransforms[t]
    14.    }
    15. }
    16.  
    So i dont need to call animator.GetBoneTransform each time.
    However, this has resulted in all Transforms being the same on each frame, as if the bone is not moving.
    As far as I know, classes in C# are passed as references, so if I store the bone transform in a variable, it should get updated , shouldn't it?
    What I'm getting wrong?
     
  2. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Hello,
    I had the same problem with Transform while doing IK. I don't understand why it does that and would be interested to know it if someone knowledgeable drops by. In the meantime, what I got is that I couldn't really read the transform directly, so I cached it everytime in Vector3 and Quaternion.

    Hope that helps