Journal 99 — Unity 2D Mobile, Dungeon Escape Set Up Spider & Skeleton

Objective: Set up spider enemy, Optimize the Enemy base class, and set up the skeleton

In this article, I will go over adding the spider as a new enemy type. In the previous article, I went over class inheritance using a base enemy class, and then each new enemy can inherit common behaviors and also have their own custom behaviors.

To start, we can set up the spider game object using one of the sprites.

Next, we can set up the animations for idle and walk.

Now, we need to fix the animation tree for switching between idle and walk. This approach will be the same as the setup for the moss giant in the previous article.

We can create two waypoints by duplicating the spider game object and then fixing the waypoint locations and then deleting the components so only the transforms are left.

The first part of the C# update is to check the current position and the target position and use Vector3.MoveTowards to move between waypoints.

The next update is to play an idle animation at each waypoint.

The idle animation is playing but the spider is not stopping at the waypoints. We can add a condition to check if the idle animation is playing, then do nothing by using “return”.

The last update to the spider is to flip the sprite by accessing the SpriteRenderer and setting FlipX to true or false depending on the direction.

Enemy Base Class Optimized

Between this article and the previous article on setting up two enemies, their code is identical. We need to optimize the Enemy base class to take care of this shared behavior so we do not need to keep writing the same code for multiple enemies. As I said earlier, we can use the Enemy base class for basic behaviors and also have the flexibility to have custom behaviors for each enemy. Note that each method in the Enemy base class uses “virtual”. Each child of the Enemy base class can “override” a particular and implement their own custom behavior.


Now that we have an Enemy base class developed, we can add the skeleton enemy. As you will see, there is much greater efficiency to adding new enemies since we have an Enemy base class.

After creating the empty game object skeleton and making the sprite a child, we can create the idle and walk animations.

After creating the walk and idle animations, we can update the animation tree, similar to the other enemies with an idle trigger and transitions between idle and walk.

We can create the skeleton waypoints by duplicating the skeleton game object twice and placing the waypoints, then deleting the components so the waypoints only consist of a transform.

Now, we can create a new C# script for the skeleton, and it inherits the Enemy base class.

Finally, we can add the script to the skeleton game object, and update the waypoints and speed in the Inspector, and try it out.

Thank you for your time!




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Raw Product — Episode 1: Launching on the Slack Directory and how we doubled signups with a simple…

Unmarshal Partners with MoonTools to build a Data Explorer for Decentralized Exchanges

What to Learn as an Embedded Developer

Deep Dive on .NET Core String.Create() Performance

Header image with the text “BOOST PERFORMANCE” and a code sample that fades in from top to bottom.

SQL Query Performance And Optimization Techniques

B-Tree index

Setting up TV Dashboards as Slideshows on Large Screens

Setting up TV Dashboards as Slideshows on Large Screens

Agile Principle #1: Satisfy the customer by releasing early and often.

Agile Principle #1

The Noob Guides: Preparing USB for Windows

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

Chris Nielsen

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

More from Medium

Journal 148 — Unity Project Development, 2.5D Shooter

Creating an Office Den in Unity Part 2: Lights, Cookies and Emissions

Making a RTS game #49: Optimisation tips & tricks (Unity/C#)

A Series On Creating Immersive Audio in Unity with Custom Wwise Scripts