How to create a recoil animation?

I’m building a basic 2D space shooter in C#. The game is working great, and now I’m trying to make it look better.

One thing that I’d like to do is a recoil animation:

  • When I fire a bullet, the spaceship prefab should move slightly backward
  • Then quickly get back to it’s initial position

How can I do that? Should I use some kind of tween? Or use the Animator? Or something else? Thanks!

You could make the recoil part of the shooting animation, but then the actual gameObject wouldn’t move, just the mesh. If you use physics, you can apply force to the opposite shooting direction / backwards. The most basic and reliable method is to use a simple Vector3.Lerp function, where you save the ship position in a Vector3 variable, then lerp to a V3 slightly backwards, and then back to the saved V3.