I am building 2D game in Unity and I’m using it’s great tools for animation. The problem is the script I wrote to move the character works only when Animator component is off. Therefore I cannot see the animation (moving legs etc). When Animator component is on I can see the animation (moving legs when arrow key is pressed) which triggered by pressing the keys, but the character isn’t moving. Can you help me to solve the problem? I don’t have any errors. I tried to build a separate actor with just one sprite and with Animator component and it works with this script. But my character with animation and with Animator component doesn’t want to work with the script. Please help me!
Is your animation changing the positions of stuff? It might be that the animation is overriding the character’s movement. You can either solve it by putting the Animator in a child object and have your motion script on the parent. Or else you can try to use root motions; select your clips and use the calculate root motion button, then enable Use Root Motion in the animator. I would go for the first solution…
Thanks for the reply!
It would be hard to use script without triggering Animator. Because once the movement is done it stops one animation and start the other one etc.
What do you mean by Root Motion?
Of course you should use the Animator. What I mean is that you should divide your CharacterRig into two parts. Have your CharacterRig where it is with everything but the Animator. Then add an empty child below CharacterRig, place your Animator on this, also move all animated parts of your character as children of the GameObject with the Animator. That way nothing that is driven by the Animator will interfere with the movement.
A bit simplified, Root Motion will cause movement in the animation clips to be added to the character. So if your animation moves the character forward one unit, it will remain there when the animation is over and if the animation loops then the character will move yet another unit. Have a look at this tutorial:
The problem is if I move Animator from the object with the script it no longer changes animation (only idling) and it doesn’t move. The layout like in the attachment doesn’t work.
But isn’t that because you have moved the Animator component and haven’t updated your character controller to look for the component in the child? Change your GetComponent() to a
GetComponentInChildren<Animator>() then the script should work as intended.
Now it works! Thanks!
