Search Unity

Question What's the best way to make a gameobject invisible during an idle animation state?

Discussion in 'Animation' started by TiggyFairy, May 9, 2022.

  1. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    505
    So I've tried making a short animation clip where the gameobject has its mesh turned off to make it invisible so it's not just frozen in the air - but that seems to knock the objects slightly out of position & is causing lag. I've also tried adding this to an idle animation node to no effect. Any ideas what I can do to make them invisible without messing up the animations? RootMotion is enabled, if it makes a difference.

    Code (CSharp):
    1.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2.     {
    3.         animator.gameObject.GetComponent<MeshRenderer>().enabled = false;
    4.     }
    5.  
    6.     //OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    7.     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    8.     {
    9.         animator.gameObject.GetComponent<MeshRenderer>().enabled = true;
    10.     }