Search code examples
c#unity-game-engineanimationassetsgame-development

How to perfectly synchronize gameobject positions in animations?


This is a recurrent question for me as if I'm not able to fix this, the alternative would be to include sprites with the same player pose for each weapon which uses the same animation. I have a spritesheet for the player:

enter image description here

For which I'm currently interested in the ones for the attack with a double-handed sword:

enter image description here

To pair with this, I have a sword spritesheet:

enter image description here

So, to use both of these in an animation, I have two game objects; one for the player (named Hero), and the other for the sword (named Weapon):

enter image description here

The animation then modifies both of these, but since the sprites are not of the same size, I employ some transition so these align so the sword looks like it's on the player's hand at all times:

enter image description here

For example, at the largest point of attack, the sword moves a bit so it stays in the player's hand:

enter image description here

When done, this animation plays but it seems that at some points the position of the sword either overcompensates or doesn't catch up, though it's not quite as visible:

enter image description here At 30fps

enter image description here At 60fps

Note that it's not too obvious, but analyzing frame by frame I find a few where the sword is out of place:

enter image description here

Now, I'm not sure if this means I'm simply being a perfectionist, but there are moments in the game where I'm implementing pauses within the game world, such as leveling up, and if the game happens to pause with one of these occasional misplacements, it will be glaringly obvious there is a problem:

enter image description here

Is it that I'm asking for too much? How could I make sure this animation works, ensuring that we have a correct transition?

I'm thinking that maybe because this animation uses several sprites, that I'm adding too many property changes. But notice that for example from 0:06, enter image description here

To 0:09, enter image description here

I change the sprite of the weapon only once, but I update its transform position on every millisecond in between. I found that doing it this way avoids having the weapon transition progressively to the other expected position, avoiding something like this which is far more noticeable:

enter image description here enter image description here enter image description here enter image description here

But is this wrong? Please let me know how I could improve this animation, and thank you for any help in advance.


Solution

  • I think I found my answer: It seems that as suspected, the issue is due to having too many transitions. Technically, it's not an issue, but I included the transitions since I didn't know the smooth position change could be modified:

    In Unity 2D, it is possible to modify the smooth transition to a new sprite by clicking on the Curves tab next to the Dopesheet:

    enter image description here

    Without the extra positioning, the animation for this attack looks like this:

    enter image description here

    And so, the curves you see there represent the system's work to adjust the position of the game object which moves in the animation (i.e. my weapon), which would be active I assume in between the milliseconds where I had added the extra positions, thus no extra work was reduced in this animation. To properly handle the positioning, I had to go to each point that actually should have a position change, for example between 0:00 and 0:03:

    enter image description here

    And grab the white edge and drag down, until the transition is no longer a curve, but a step. Following along the rest, the entire animation "curve" would look like this:

    enter image description here

    And so, when running the animation in loop, you'll notice that the position is perfect

    enter image description here

    Thus far, running it in game mode doesn't reveal any sword misplacement, so I should ensure any other animations I have are modified this way to avoid oversaturating the game with internal work.

    Hopefully this helps anyone stuck on the same thing! But do note that this works for me because my animation is strictly spritesheet based, thus it might not be as ideal if using any other tools like mecanim.