Journal 97 — Unity 2D Mobile, Dungeon Escape Attack Animations and Follow Camera

Objective: Add several attack animations and add Cinemachine virtual camera to follow player.

In the previous article, I added the basic walk, run, and jump animations. In this article, I will add additional animations for various sword attacks and add a Cinemachine virtual camera to follow the player.


We can start with the regular attack animation.

We can first create a new attack animation in the Animation window. This new attack animation will automatically be added to the Animator window, and we can add an “Attack” trigger and transition between Idle and Attack.

Now we need to implement the attack animation via code. We can add a public void method in the PlayerAnimation script to trigger the attack animation. In the Player script, we can check for the left mouse button and also whether the player is grounded.

Here are the results.

Attack while running

Now let’s add the option to attack while running.

Sword Arc Animation

Next, we can add a sword arc animation.

We can drag the first sprite onto the Player in the Hierarchy and then create a new animation in the Animation window.

Next, we can modify the speed of the animation and move some of the animation frames slight to create a pause. Also we we can modify the orientation of the animation since our sword swing is more horizontal.

In the Animator window, we can create a default empty state that is “None”, and a trigger for the sword animation.

In the C# script, we can get a reference to the sword arc animator component by using transform.GetChild(1).

Sword Arc Animation Cleanup

To better align the sword attack with the sword arc animation, we can reduce the number of key frames on the regular sword attack. We can go to the Animation window and delete the sword attack animation frames, and just use a few frames. We can also adjust the position of the sword arc.

Sword Arc Flip

As you can see, the sword arc animation does not flip when turning left. We can fix this by accessing the Sprite Renderer FlipX and FlipY properties, and also change the x-position of the sword arc animation to the -x position (i.e., if it is +1.01 when facing right, it should be -1.01 when facing left).

Now let’s see the results.

Camera Follow

Now that we have all the player function pretty much set, we can add a camera follow system using Cinemachine, which can be added by going to the Window drop down menu →Package Manager.

After Cinemachine is added, we can create a new virtual camera, and drag the Player game object to the virtual camera “Follow”.

Here are the results.

Thank you for your time!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.