Search Unity

Strange behavior when disabling animator via code vs inspector

Discussion in 'Animation' started by nobluff67, Jan 15, 2020.

  1. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    Setup
    1. I have a basic idle animation on loop.
    2. character is ragdoll ready, but not currently active.
    3. I detach foot through script (detach character joint, change rigidbody from Kinematic to Gravity, activate collider)
    4. foot detaches.
    5. foot wonders off into the distance (ie, doesn't adhere to gravity, it seems to be somehow still linked to animator, yet it doesn't follow animation). It does recognize collider.
    I have attempted to fix foot wondering off by:

    1. Switching animator off via code - Foot moving adheres to gravity. Start animator again via code, foot starts behaving strangely again (ignores gravity).
    2. Switching animator on via inspector. Foot movement adheres to gravity. Start animator again via inspector, foot behaves as expected (adheres to gravity).
    Does anyone have any idea why code manipulation of animator, behaves differently to inspector manipulation?
     
  2. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    Solved it though code by using this. (No idea what Rebind does, or if I need it, just copied it from this answer for the immediate solution.

    https://answers.unity.com/questions/1009189/how-can-i-change-a-controllers-model-at-runtime-wi.html

    Thanks to @MamaCatDev for posting the above question, and then answering it. Different question, yet the solution worked for me.

    GetComponent<Animator>().avatar = AvatarNone;
    GetComponent<Animator>().Rebind();

    GetComponent<Animator>().avatar = MyAvatar;
    GetComponent<Animator>().Rebind();