How to animate the relative position of a sprite during a sprite animation?

To any animation experts out there - I have a basic sprite animation that I’ve created from a sprite sheet. The animation reuses some of the frames but I want them to be in slightly different locations each time they appear (kind of like a shake effect). Rather than shake the entire GameObject (and its collider etc) I’d rather just make the animation show a shake.

So rather than duplicating a whole bunch of frames in different positions, I’m trying to adjust the relative position of some of the frames as the animation plays. I’ve already worked out that this isn’t really supported directly by Unity because Transform animation keyframes are absolute, not relative, so the naive approach just ends up sending the entire object back to the same place in the World.

The workaround I’ve read about in several places is to put the object as a child of a parent, and put the Animator on the parent. This allows me to change the relative position of the child as the animation plays, giving it a shake.

But when I do this, my original sprite animation does not play - presumably because the Sprite Renderer and Animator need to be on the same object?

So what’s the solution to this? Do I need to create two Animators, one on the parent for the position animation (shake effect) and another on the child for the actual sprite animation? If so, how do I get them to sync up and keep them in sync, given that they will be using different Animation assets and therefore can’t be edited together?

What am I missing here?

Hi,

The SpriteRenderer needs to be on the child because that’s the component you want to animate. Whether the Animator is attached to the child or the parent does not matter but it is good practice to attach it to the parent, so it is easier to find it again.

When you animate the child game object, make sure to animate the transform.localPosition, not the transform.position. Also, for testing purposes, don’t use too high or low values but rather values close(r) to 0.

Hopefully, this helped. :slight_smile:


See also:

1 Like

1 Not an expert.
2 Not entirely sure I understood what you are trying to achieve but I have setup a animation you see below. It is made using a single animation on the ant sprite itself where I have both the sprite as well as the position animated in the same animation. The ants head+antenna is changing via the changing sprite frames while the jerky movement is via the position changes. The white background is a separate sprite . The ant sprite and background are set as children of another gameobject so as to be able to move the whole thing as one unit.

image
2021-06-14 23-05-10_150-300

1 Like

Thank you @Nina and @codermonk - I’ll need to try again in a fresh scene because the issue I’m seeing is that after creating a child object and moving the sprite renderer to it, the sprite animation is not playing. However I am able to get the local position changes to animate.

At least now I know it’s possible to do it, which is half the battle. I’ll report back if I find out anything interesting.

1 Like

How are you getting on with this, @piwakawaka?

Hi @Nina, sorry I’ve been distracted by other things. I’ll revisit this sometime but I’m comforted to know it can be done, so you can close this as solved if you like :slight_smile:.

The thread will close automatically after an answer has been marked as “solution”. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms