Using Animator.Play twice in the same function doesn't bring expected results.

Hi,
I am trying to read the state of the rig in a certain frame of an animation. My idea is for each frame to invoke animator.Play(“anim 01”,0, Time0) and read the data but it doesn’t work. The play() function doesn’t even seem to be executed in the start().
It’s crucial for me to do it at once without update().

My code is below. Is there a way to implement it correctly?

{
SearchBones(rigSource); // finds name of the bones in the rig

float nT1 = 0.32f;
float nT2 = 0.75f;
string boneName = “MCH-torso.parent/torso/MCH-spine.002/spine_fk.002/MCH-spine.003/spine_fk.003/tweak_spine.003/ORG-spine.003/ORG-shoulder.R/DEF-upper_arm.R/DEF-upper_arm.R.001/DEF-forearm.R”;
Transform sourceJoint = GetJointByHierarchyName(rigSource, boneName); // return rigSource.Find(jointHierarchyName);
animator.Play(“anim 01”,0,nT1);
Debug.Log("Time0: " + sourceJoint.localEulerAngles);
animator.Play(“anim 01”, 0, nT2);
Debug.Log("Time1: " + sourceJoint.localEulerAngles);
}

It seems like you are trying to read the local Euler angles of a specific joint in the rig at different times during the “anim 01” animation. If the animator.Play() method doesn’t seem to be executed in the Start() method, there might be a couple of things you can check and adjust:

  • Awake() vs. Start(): Ensure that your script is attached to an object with the Animator component and that the Start() method is the appropriate place to trigger your animation. If not, you might want to try using Awake() or Update() depending on your needs.

  • Animation Loading Time: If the animation is not fully loaded or started during the frame when Play() is called, the subsequent calls might not work as expected. Ensure that the animation is loaded and playing before attempting to read the joint data.

  • Coroutine or Callbacks: Unity might not immediately update the animation state after calling Play(). You might want to consider using coroutines or animation event callbacks to ensure that the animation has reached the desired frame before attempting to read the joint data.

Here is a modified version of your code using coroutines:
void Start()
{
SearchBones(rigSource);
StartCoroutine(ReadJointDataAfterPlay());
}

IEnumerator ReadJointDataAfterPlay()
{
float nT1 = 0.32f;
float nT2 = 0.75f;

string boneName = “MCH-torso.parent/torso/MCH-spine.002/spine_fk.002/MCH-spine.003/spine_fk.003/tweak_spine.003/ORG-spine.003/ORG-shoulder.R/DEF-upper_arm.R/DEF-upper_arm.R.001/DEF-forearm.R”;
Transform sourceJoint = GetJointByHierarchyName(rigSource, boneName);

// Play the animation at time nT1
animator.Play(“anim 01”, 0, nT1);
yield return new WaitForSeconds(0.1f); // Adjust the delay based on your animation length
Debug.Log("Time0: " + sourceJoint.localEulerAngles);

// Play the animation at time nT2
animator.Play(“anim 01”, 0, nT2);
yield return new WaitForSeconds(0.1f); // Adjust the delay based on your animation length
Debug.Log("Time1: " + sourceJoint.localEulerAngles);
}

Adjust the WaitForSeconds delay based on the length of your animation to ensure that Unity has enough time to update the animation state.

stbemu codes daily lists