Switching Animator Controller via scripts at runtime resets player's Transform

Hi.

In toying around with Animator component on a skinned mesh, I discovered that for some reason, when NOT using root motion (works fine otherwise) and switching the runtimeAnimatorController property via code somehow resets the entire player’s transform object (position and rotation) to where it was in the world when Play mode began.

The code for swithing used to be:

PlayerAnimator.runtimeAnimatorController = //new controller here

I managed to work around it using the following wrapper function:

Vector3 position = PlayerTransform.position;
        Quaternion rotation = PlayerTransform.rotation;
        Vector3 velocity = PlayerRigidbody.velocity;
        PlayerAnimator.runtimeAnimatorController = //new controller here
        PlayerTransform.position = position;
        PlayerRigidbody.velocity = velocity;
        PlayerTransform.rotation = rotation;

Again, it used to be OK when I was using Root motion for the avatar’s locomotion, but I decided to switch root motion off and drive the player’s Rigidbody manually in order to have more control over what’s happening, since it’s going to be a relatively complex third person RPG.

Any ideas what’s causing this?

EDIT - Added velocity saving to the function as well.

1 Like

bump got same issue. They change how the whole hierarchy updates when you change the controller (dont know why they did this)… pretty useless now…because the fix would be glitchy.

I believe this is an unintended bug. It’s weird how the skinned mesh resets to where it was when the scene began playing, not (0,0,0) as you would expect. You move your player’s starting point and it gets reset there.

Also, it seems my little workaround posted in the opening post is actually working rather nicely. Simply storing and restoring position and rotation appears to be enough. Physics look solid, no glitches, no unintended side effects.

2 Likes

Yeah your fix works i am using it now, but we shouldn’t be making such workarounds in the first place.

You can see the bug is there even when your not doing it by code. Just inside the editor manual changing the controller.

Eh, we’re in game development. It’s part of the job description :smile:

Dunno about you, but the fact that I am switching animation controllers at runtime is already a workaround, to avoid having hundreds of animator parameters, spaghetti controller code, and spaghetti state machine.

Now I’ll have to workaround the jarring, non-interpolated animations when switching controllers.

After all that, I’m gonna have to workaround something else.

2 Likes

Your right but its a annoying new “feature” :stuck_out_tongue: Thought i am happy with the performance in 5.5.1

I have seperated controller for each weapon type, to avoid the mess in the nod Mecnamim . I show you how it did this pretty clean with enums. I am also working on a RPG :slight_smile: its all about flexibility and control right?

    public void SetAnimationMode()
    {
      
        if (combatMode == Condition.Melee)
        {
            ChangeController(CharAniMelee);
        }
        else  if (combatMode == Condition.Ranged)
        {
            ChangeController(CharAniRanged);
        }
        else  if (combatMode == Condition.Ranged2Hand)
        {
            ChangeController(CharAniRanged2Hand);
        }
        else  if (combatMode == Condition.Mount)
        {
            ChangeController(FlyStance);
        }
        else
        {
            ChangeController(CharAni_None);
        }
//        RefreshAniControllerContent ();
    }

    public void ChangeController(RuntimeAnimatorController Newctrl)
    {
        Vector3 position = Player.Script.transform.position;
        Quaternion rotation = Player.Script.transform.rotation;

        anim.runtimeAnimatorController = Newctrl; 
        Player.Script.transform.position = position;
        Player.Script.transform.rotation = rotation;
    }
1 Like

Just so every one knows. This is not permanent fix for this bug, the bug must be fixed else i see no point on using a newer version of Unity Engine. This leads to all strange things and glitches where the object transform allaround the place and back on Animation controller change… Animation controller is a pretty core feature in Unity…

Well, I’d argue there’s some significantly worse stuff that needs fixing before this. It’s not deal breaking.

In my RPG, we started off by using a skeleton rig from Third Person Camera asset pack. It got significantly modified with weapon bones, entire animation control code re-written (physics driven instead of root motion etc).

Some of the work on that rig was done in 3DS Max by me. A friend who is also interested in animating does not have a Max licence, so he’s learning to use Blender (to be honest, Blender has way superior animation workflow).

When he exports an animation from Blender, it’s impossible to use “Copy Existing Avatar” from Rig tab in import animation (I mean, you can use it, but it just goes Gmod Idiot Box on you). It’s Created anew from import.

Here’s the kicker:
You cannot put his (Blender) animation into an Animator built from previous Avatar (made in Max). It just shows up blank (idle animation). You cannot mix animations with different avatars in one Animator controller. Sound somewhat logical, yes?

BUT, if you put his animation into a new Animator controller, and just switch the runtime animator controller via code, it auto-magically works! Animation looks correct, feels correct, skeleton and entire rig is correct. I dunno how, I dunno why.

Why can’t this “retargeting” logic be applied to “Copy Existing Avatar” workflow, I do not know.

1 Like

When you import a new animation that has the same physics and structure as your old Avatar. you can copy that avatar to your new Humanoid skeleton it works just fine. You dont even need to import the mesh.

I haven’t tried with the new version yet, maybe i can’t do that any more ether. I really wonder why i upgrade in the first place…

Edited the opening post. Velocity from the rigid body also needs to be saved, otherwise it gets lost as well. That’s a bit weirder now.

Man i tell you, your fighting a war you can’t win… the bug war, featured by Unity. My bug report was moved to animations section so i guess this bug is part of the animation now. we suppose to warp to 0 now… /sarcasm off

This will always glitch nomatter how much you try to preserve data, you should see what one of my AI mob did when the controlller changed… Monster was like…“wtf is this player doing”… warping…glitches…I am not even talking about multiplayer issue this also effects. Maybe i should go back to legacy animation…

I just got this too. I submitted a bug report with a repro case. I doesn’t happen when I switch the runtimeAnimatorController, but if I switch it and then switch it back, my character instantly pops back to its starting position.

@Eudaimonium @Ironmax @kdgalla Also changing the avatar at runtime apparently doesn’t work. So how do we allow players to switch characters in the middle of the game? There are workarounds I suppose, but in my case it would be a nasty workaround given the way I currently have things set up (since I didn’t know about these bugs until now).

I am pretty sure 5.3.1 did not have this new feature “bug” doesn’t look like they even bother to fix it.

What are we supposed to use as a workaround? Allowing a change of characters is kind of a basic function. One person suggested rebinding the animator after resetting the parent and localposition: http://answers.unity3d.com/questions/1009189/how-can-i-change-a-controllers-model-at-runtime-wi.html

Bump this animation controller issue is still broken in 5.6.1

Well this is fixrd for unity 2017.1 but had to subscribe to be able to continue publishing to android :frowning:

Is this still not fixed for 5.6x versions?

nope