Gun Recoil Animation in Unity

To finish off the gun we've been working on in the previus lessons, let's add a recoil animation. We'll keep it simple. We'll take the initial position of the gun, move it back a bit, and then return it to its initial position.

So, first, create a new folder under Assets in the project area at the bottom of Unity. Call it Animations. Double click to move inside of this folder.

To create an animation, display the animation panel by going to Window at the top of Unity. From the Window menu, select Animation > Animation:

Unity menu animation

You should now see a new panel at the bottom of Unity: (If it's a separate panel, drag it down to the bottom, where the Project panel is.)

The Animation window in Unity

The panel will say that no animation object is selected. So you need to select your gun in the Hierarchy (the top level item for the gun, which is M1911 Handgun_Black, in our case).

When you select your gun object, you'll see this in the Animation panel:

The Animation wndow with a game object selected

So, to begin an animation, you need to click the Create button. When you do, you'll see a Save dialog box appear. Type GunRecoil as the name of the animation:

The Create New Animation dialog box

If you look inside of your animation folder, you should see two icons for your animation:

A Unity Project folder showing two files

But click back onto the Animation tab. (Make sure your gun is still selected in the Hierarchy.)

Your Animation tab will have changed. It will look like this:

Unity Animation timeline

The area on the right is a timeline. The numbers at the top are frame numbers. (The 1 on the right represents 60 frames.

We want to move our gun back, in the Z direction. So our animation needs to manipulate the position, which are those values in the Transform area of the Inspector. To do that, click the button on the left for Add Property. You'll see a popup:

Adding a position property to the timeline

Expand the Transform item and then click on Position. Then click the plus symbol, as indicated by the red arrow in the image above.

When you click on the plus button, the Animation tab will change to this:

The various areas of the Unity timeline

There are a few areas to note, here. If you look at the red box labelled 1, in the image above, you'll see the current position of the gun. These are the same as in the Inspector on the right.

The arrows 3 and 4 show that Unity has taken these position values as a start and end point for your animation. You don't have to accept these values, but as they are fine for us, we can leave them.

The red box with the number 2 next to it is the current frame number. Type a new value here and the timeline will jump to that frame number you typed.

Although the end point values for our animation is correct, the frame number is not. It's on 1 (60 frames). So click on the 1 to go to that frame number. The timeline turns blue:

The timeline showing 60 seconds of animation

Now click on the white diamonds. They will turn blue. Hold your left mouse button down on the diamonds.

The end point of the animation in the timeline

Keep your left mouse button held down and drag left to frame number 0.25:

Setting the animation end to 25 frames per second

Click in the timeline, at frame 0.25. The vertical line will move and show you that you're on frame 25, as in the image above.

What we've just done is to set the start position of the gun and the end position. These are the same values, as shown on the left. But we need the gun's Z position to go back a bit. So, type 13 into the text box at the top, where it currently says 25. Press enter and the white vertical line will jump to that frame:

Setting the mid-point of the animation to frame 13

You can change the Z position a few ways. You can just type a new number in the Position Z box on the left. Type 0.25.

Another way to move the gun is to move the Z arrow in the Scene, just above the animation:

Using scene view to set the animation

(If you move the gun back in the scene, though, the new position won't register in the timeline. You'll have to click inside the Position Z text box and press enter on your keyboard.)

When you're happy with new position, click into the Position Z box and press enter. You'll see white diamonds appear at frame 25:

The Z position of a game object being indicated for the animation

If you make a mistake, click on the diamonds to highlight them. Then press the delete key on your keyboard to remove the position in the timeline.

And that's it - we've completed our simple gun animation. To see what it looks like, either press the play button at the top of the Animation tab, or drag the white vertical line back and forward:

Playing the animation via the Unity timeline

If you actually play your game, you'll see the animation is applied. However, it's not what we want, as the video below shows (6 seconds):

 

The gun is moving back all the time, and it looks like we've come down with a case of palsy! Let's solve that because we only want the animation to play when the gun is fired.

To get your animation back, click on your Animations folder in the Project area at the bottom of Unity. Now click on GunRecoil to select it. In the Inspector on the right, uncheck Loop Time:

Uncheck loop time in the Inspector

There's just one more thing to do before we can write the code to play the animation.

Inside your Animation folder, click the second icon to select it:

The Animator Controller highlighted in Unity

The second icon is the Animator Controller. With this second icon selected, have a look at the Inspector on the right. Click the Open button:

Opening the Animator Controller via the Inspector

This will open up a new tab at the top, the Animator tab. It will look like this:

The Animator tab shwoing three states

The blocks are called States. At the moment, we're going from the Entry state to the Gun Recoil state. But we need to add another state just to pause things. So, right click inside the Animator tab. From the menu, select Create State > Empty:

Creating an empty state in the Animator tab

When you click Empty, you'll get a New State. Click on this new state to select it. Then change its name in the Inspector on the right. Call it GunPause:

Renaming an animation state

We now need to reset the default state, which is pointing at GunRecoil. We're doing this because we don't want the animation to play immediately. So, right-click on GunPause. From the menu, select Set as Layer Default State:

Setting a Layer as the Default State

When you reset the default state, the orange line will point to GunPause:

The entry state pointing to another animation state

OK, we're done! Save your work and we're ready to code.

 

Play the Animation

The code we're going to write will play the animation when the gun is fired. So, go to your Scripts folder. Double click your GunFire script to open it up, if it's not already open.

There's not much to add. First, though, we need a new variable at the top of the code. Add this line below the three you already have:

private Animator gunAnim;

This is an Animator variable type that we've called gunAnim.

Next, we need to get the Animator Component attached to the gun. You got an Animator added by Unity when you added the recoil animation to the gun. It's this one, in the Inspector:

The Animator component attached to a gun game object

We now need to get a reference to this with code. So, add this line to your Start method:

gunAnim = GetComponent<Animator>();

Easy stuff - get the Animator component and store it in the gunAnim Animator variable from the top of the code.

Now we can add a couple of lines in the FireGun IEnumerator. Just below, hasGunFired = true, add this line:

gunAnim.Play("GunRecoil");

We want to play the GunRecoil state from our Animator tab. This will change it from the GunPause state we set up, and which is the default.

After the yield line, we can set the state back to GunPause. Add this line then:

gunAnim.Play("GunPause");

The line is almost the same as the other one, except we have GunPause between the round brackets of Play.

But your code should look like this (new lines are highlighted):

Unity C# code to get an animation and play it

Save your work and go back to Unity. Now play your game. Fire your gun and you should see the recoil animation play, as in the video below:

 

We're getting somewhere! Now let's get an enemy to shoot at.

<--Back to the Unity 3D Course Contents Page