Change the y position of a prefab in an animation?

I’m building a simple 2D space shooter in C#.

When the spaceship fires a bullet, I want to create a recoil effect: the spaceship moves slightly backward, then goes back to it’s position.

I tried doing this with animations, but it doesn’t work. It kind of make sense: in the animation I’m changing the position of the prefab, but I’m also moving it position when moving it around.

So how can I create an animation that changes the y position of a pefab while still being able to move the prefab around the screen?

Thanks!

Perhaps make the spaceship a child of an empty gameobject, moving that around with your controls instead. Then create a animation only for the child spaceship, hopefully it should move separately.

Hope this helps!