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.

Skeleton

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!

--

--

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