Sunday, January 7, 2018

c# - Unity 2d Animation from spritesheet: How can I move the character according to the animation?


I am trying to implement an attack animation that should end up in moving the player by some distance. The animation would be something like this:


enter image description here



As you can see the position of the player should be different by the end of the animation, but I am not sure how to make it so. I tried to use root motion, however even if in the animator component i set "Apply root motion" to true, when I click on the animation itself, there is no option to use root motion, perhaps is it due to the fact that the animation is obtained from a spritesheet and not from a rigging software like Spine or DragonBones? Anyway, is there another way to update, either by scripting or throught the animation window, the character's position after the animation?



Answer



Yes, the "apply root motion" flag requires a rigged 3d character model to work.


But the nice thing about the Unity animation system is that you can use it to animate almost any numeric public property of almost any component, including the Transform.


When you are editing the timeline of your Animation with the player game object selected, you can add the Transform as a property and create a curve for the Position.x.


enter image description here


However, I would really recommend you to edit the spritesheet and keep the character centered in the center of the animation cell during every frame. This gives you far more flexibility because you can easily tweak the range of that attack motion in the Unity editor. If your game is an action-oriented game, then the exact distance the character travels when performing that attack is relevant for gameplay. That means it's something you might want to experiment with and iterate on quickly. And then there is also the issue with making sure the collider is where the player would expect it to be during every animation frame. If the character moves around in a sprite which is far larger than itself, then that's quite tedious to get right.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...