Moss Giant AI in Unity

In this article, I will be going over how I created the AI for my moss giant.

Getting the Moss Giant Moving

To start, I duplicated the moss giant and created two-waypoints for them to walk between. The duplicated moss giants should be empty.

In the enemy abstract class, I added variables for the waypoints and an abstract method.

Inside the moss giant script, I added the abstract method and created the logic to move the moss giant. The logic is to check to determine if the moss giant is at a waypoint, if it is you set the current target to the opposite target. Then you set the moss giant to move toward the current target.

This gets the moss giant moving between the waypoints.

Fixing the Animations

I wanted the moss giant to stop and do the idle animation before going to the other waypoint.

To do this I got a reference to the animator and triggered the idle animation when the moss giant reached a checkpoint. In the update method, I created an if statement that would check if idle was playing. If it was the method would return and the movement script would not get called.

This got the result I was looking for.

The last thing to do is to flip the sprite to face the correct direction.

I created a boolean variable in the enemy abstract class to check which way the enemy is moving.

In the moss giant script, I got a reference to the sprite renderer.

I set this boolean variable false in the moss giant script when the moss giant is moving right and true when it is going left. I created a method that would flip the sprite based on this variable. Then I called the method right before the movement method in the update function so the moss giant would flip after playing the idle animation.

This made the moss giant do exactly what I wanted it to do.

--

--

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