Search Unity

Swaping mesh (or prefab) with AIThirdPersonController

Discussion in 'Animation' started by DavidDraeyer, Mar 25, 2020.

  1. DavidDraeyer

    DavidDraeyer

    Joined:
    Jan 14, 2020
    Posts:
    3
    Hi,

    I'm new in unity, and I didn't find my answer in this forum or using Google. Perhaps I don't have the good keywords...

    I'm animating a character controled by the computer with the AIThirdPersonController of Standart Assets. I did replace Ethan by a prefab from Little Heroes Megapack, and everything is working fine.


    Now I would like the character to transform into another character. I did try to remove the prefab from the AIThirdPersonController, and load a new one, from the Little Heroes Magapack too.

    Code (CSharp):
    1. // Just to get some information of the animator component... unusefull for now...
    2. Animator anim = GetComponent<Animator>();
    3. Debug.Log(anim.avatar.ToString());
    4.  
    5. // Instentiation of the new prefab  
    6. GameObject perso = Instantiate(avatar, transform.position, avatar.transform.rotation);
    7.  
    8. // Prefab to remove
    9. var old= transform.GetChild(0).gameObject;
    10.  
    11. // Copy properties
    12. perso.transform.parent = old.transform.parent;
    13. perso.transform.localPosition = old.transform.localPosition;
    14. perso.transform.localRotation = old.transform.localRotation;
    15. perso.transform.localScale = old.transform.localScale;
    16. // Place in the same place        perso.transform.SetSiblingIndex(old.transform.GetSiblingIndex());
    17.  
    18. // Destroy and rename to be exactly the same
    19. var temp = old.name;
    20. DestroyImmediate(old);
    21. perso.name = temp;
    The new one is effectively loaded, but not animated. Everything seems to be the same, but a connection is probably missing.

    How can I do that, or where can I find documentation? I tried documentation of the animator component, but It doesn't seems to be there...

    Thanks!

    David
     
  2. DavidDraeyer

    DavidDraeyer

    Joined:
    Jan 14, 2020
    Posts:
    3
    Probably the wrong thread... sorry. I post it in "Scripting", too.
     
  3. Ex6tra

    Ex6tra

    Joined:
    Oct 31, 2019
    Posts:
    14
    Choose your imported player mesh and generate an avater in the rig tab from the inspector, after that go to the Animator component on the AIThirdPersonController and choose the new Avater generated by the rigging process, note that your new player model should have bones you can add them from mixamo