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.

Question Animator is playing but object is not animating

Discussion in 'Animation' started by CystemCV, Oct 28, 2022.

  1. CystemCV

    CystemCV

    Joined:
    May 7, 2022
    Posts:
    25
    Is there a way to restart animator from script?

    What I am trying to do :

    I have my player gameobject.
    upload_2022-10-28_22-8-26.png upload_2022-10-28_22-8-41.png

    I want to keep everything as it is but change the Root (which is the object that has all the animated parts) to a different Root. (It's how i want it to have 1 prefab that will be used for multiple playable characters)

    I achieved that with few lines of codes like below. I run the game and the old Root is gone and the new Root which I get from a scriptable object is now the new child Root of the Player gameobject.
    It is changed correctly but the animations just dont work.

    upload_2022-10-28_22-11-54.png upload_2022-10-28_22-12-40.png

    The thing is when the game is running and I turned the animator on and off my character animates...when I try to do it from script it just doesnt work...Any ideas?
    upload_2022-10-28_22-13-19.png
     
  2. CystemCV

    CystemCV

    Joined:
    May 7, 2022
    Posts:
    25
    Well...after a lot of research.

    With the below the animator restarts and binds the new Root that contains the new animated objects.(all have the same name as the old Root)

    I had to do a coroutine with slight delay otherwise the animator was not restarting
    Code (CSharp):
    1.     public void InitializePlayerModel()
    2.     {
    3.  
    4.  
    5.         //root contains the objects we need when we create a new character from character creation asset
    6.  
    7.         //get the current root
    8.         GameObject currentRoot = this.gameObject.transform.Find("Root").gameObject;
    9.  
    10.         //destroy it
    11.         Destroy(currentRoot);
    12.  
    13.         //get the root from the scriptable object
    14.         GameObject characterModel = Instantiate(so_Player.characterCreationModel, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
    15.         GameObject rootObject = characterModel.transform.Find("Root").gameObject;
    16.         rootObject.transform.SetParent(this.gameObject.transform);
    17.  
    18.         //remove the characterModel that is uneeded
    19.         Destroy(characterModel);
    20.  
    21.         //set the root position
    22.         rootObject.transform.localPosition = new Vector3(0, 0, 0);
    23.  
    24.         rootObject.transform.localScale = new Vector3(1, 1, 1);
    25.  
    26.  
    27.         StartCoroutine("InitializePlayerModelDelay");
    28.  
    29.     }
    Then inside the coroutine
    Code (CSharp):
    1.     public IEnumerator InitializePlayerModelDelay()
    2.     {
    3.  
    4.         yield return new WaitForSeconds(0.01f);
    5.         Animator animator = this.gameObject.GetComponent<Animator>();
    6.         animator.Update(0);
    7.         animator.Rebind();
    8.  
    9.     }
    This worked for me...don't know if it's the best choice or not but it works