Simple Enemy Behavior in Unity

Siddhant Thakur
Nerd For Tech
Published in
4 min readMar 28, 2021

--

You’ve created a Player Movement, and have made sure that your Player fires a Laser, But there’s still something missing i.e. you have nothing to shoot at or nothing to move towards or away from, so let’s do something about it.

Phase-I

Lets start off by creating an Enemy Prefab(to learn more about prefabs click here).

With that done, lets give the Prefab some color.

Edit the Scale of the object if you wish to.

Phase-II

Adding Movement to the Enemy Prefab.

As discussed in the Laser Firing article, whenever there’s an object that needs a behavior we add it by creating it’s own behavior script.

Let’s start off adding some vertical movement.

Speed at which the Enemy falls. It’s Serialized, so that the designers can change the value through the editor.
Vector3.down is equivalent of new Vector3(0, -1, 0).

Make sure to add Movement() to your Update().

This should give you the following.

There’s a small problem with the implementation above, we notice that the Enemy keeps travelling downwards. One simple fix to this could be to reset the Enemy’s position to what it was prior travelling downwards. But in doing so another problem arises, that being the Enemy keeps looping it’s movement at the same spot.

To counter this we can alter the Transform of the Enemy along it’s X-axis, and have it to spawn at random points along the X-axis.

To pick Random values within a range we can use.

This takes two parameters, the minimum value and the maximum value(Both Inclusive).

With this we can alter the Position by doing the following.

where _randX is our random x-axis value to spawn our Enemy. The if() statement checks if we’re off the screen, and if we are we reset the position to (RandomX val, OriginalY val, 0).

This gives us,

Phase-III

With the Enemy moving, we can now finally add collisions into the game. In this case, we are going to set the enemy as a trigger and have the Player and the Enemy have Rigidbody’s with Use Gravity toggled OFF. To learn more about triggers click here, and to learn about Rigidbody’s click here.

To detect collisions with triggers we can use the following,

Where “other” stores the information of the collider that collided with the Enemy’s collider.

So on doing the following,

This should print the name of the game object that collided with the trigger.

we get,

We can see the names of the colliders that come in contact with the Trigger.

Now all we have to do is destroy the game objects when they come in contact with each other. This can be done with the following,

and with this we can do the following,

But I’d hold off from destroying the Player game object just yet.

With this we have a basic Enemy behavior set up. This enemy behavior was achieved by the following.

More to come as I learn more about spawning objects.

--

--

Siddhant Thakur
Nerd For Tech

Aspiring Game Developer with experience in the field of Machine Learning.