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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Mirror animation clips with C#

Discussion in 'Animation' started by basementApe, Mar 22, 2022.

  1. basementApe

    basementApe

    Joined:
    Oct 2, 2013
    Posts:
    19
    Couldn't find any info on how to do this in code. I've got a model with an Animator controller assigned to it, but I'm controlling the animations themselves from script, like this:

    Code (CSharp):
    1.     void Animate_Loop(int maxFrame, float fspeed, string clip)
    2.     {
    3.         if (frame < maxFrame)
    4.         { frame += fspeed * Time.deltaTime; }
    5.         else
    6.         { frame -= maxFrame; }
    7.  
    8.         frameTimeIndex = Mathf.Floor(frame) / maxFrame;
    9.  
    10.         anim.speed = 0f;
    11.         anim.Play(clip, 0, frameTimeIndex);
    12.     }
    13.  
    (... and if you're wondering why, it's because I want to disable frame interpolations.)

    This works fine. I'm handling all the transitions etc in code and not controlling anything from the editor.

    But I don't see a way to mirror my animations in C#. The normal way to do it seems to be to use a bool to toggle mirroring in the Animator editor window, but that didn't work for me, i.e. this...
    Code (CSharp):
    1.          if (xInput > 0)
    2.             { anim.SetBool("isMirrored", false); }
    3.             else if (xInput < 0)
    4.             { anim.SetBool("isMirrored", true); }
    .. does nothing at all. I already set the boolean up in the Animator controller.

    Any way to do this directly in code? Something like Animator.mirror = true?
     
    Last edited: Mar 22, 2022
  2. basementApe

    basementApe

    Joined:
    Oct 2, 2013
    Posts:
    19
    Ah found the problem. For mirroring to work I need to enable the humanoid rig on the imported asset.