Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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

Discussion in 'Animation' started by rudy10003, Jan 7, 2024.

  1. rudy10003

    rudy10003

    Joined:
    May 29, 2023
    Posts:
    2
    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);

    }


    unit.png
     
  2. glenphillips21

    glenphillips21

    Joined:
    Jan 14, 2024
    Posts:
    1
    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:

    1. 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.

    2. 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.

    3. Coroutine or Callbacks: Unity might not immediately update the capcut mod apk 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.
     
    Last edited: Jan 17, 2024