Issues with rig animation when flipping local scale.x direction on game object

I am creating a 2D game and the enemies in the game have rigid body 2D connected to them.
I have not worked on the game for almost a year and when I started coding again I was forced to update Unity to the latest version (because I had upgraded iOS on my Mac).

An unexpected bug appeared. When I change direction of my enemies through a script the animation/animator changes its y position. I suspect changing localScale.x with -1 also affects the position of the animation/animator.

Correct:
107147-corret.jpg

Wrong:
107148-wrong.jpg

The enemies in my game are flipping when OnCollisionEnter2D is called.

    Vector3 enemyScale = transform.localScale;
    enemyScale.x *= -1;
    transform.localScale = enemyScale;

It’s only when enemyScale.x == -1 that the animation/animators Y-position is off. As soon as OnCollisionEnter2D is called a second time and the enemy returns to it’s original localScale the animation/animator jumps back to it’s correct Y-position.

Anybody else who have encountered this problem?

UPDATE

It’s seems like the root position of the Animator is changing. Still not closer any solution.
Any help would be grateful.

You may be able to work around this by changing your object hierarchy around. I’m guessing you have something like this:

Root Object (w/ animator, collider, and collision response script) ---arm ---leg ---body ---....

Try changing it to:

Root Object (w/ collider and collision response script) --- base (w/ animator) ------ arm ------ leg ------ body ------ ...


This way the object the animator is on doesn’t change when the scale changes. I’ve fixed many strange problems like this by moving all rendering/animating to children objects.

Thank you for your reply.

I actually have the hierarchy you refer to.

Root Object (w/ collider and collision response script)
--- base (w/ animator)
------ arm
------ leg
------ body
------ ...

That’s why I find this behavior so strange.

I solved my problem by changing the rig animation type from Humanoid to Generic.