Aggressive Enemies- Game Dev Series 116

Objective: make all enemies would automatically attack Player when in range.

In this article, we would make all enemies will attack Player when Player is in range. Before this function, the enemies are passive on attacking besides they were attacked.
It would need 2 parts to finish this function:

  • A new attacking animation that would be triggered by any state.
  • The timing of calling the attacking trigger.

Attack animation state

First we need a new animation state to all enemies.
We can simply create one by coping the Attack state animation and paste it back to Animator. Then rename it as “DetectAttack” and set the transition from Any State to it.

Next, create a trigger called "DetectPlayer” and assign it to the transition from Any State to DetectAttack.

Set the transition from Any State to DetectAttack with no Exit Time and set the transition duration to 0.
Then from DetectAttack to Idle, keep Exit Time and also set the transition duration to 0.

Once done, we can adjust the code to make this works.

Attack when detected

Open Enemy script, and first we need a bool to start the animation only 1 time per detection.

Then create a new method to trigger enemies start attacking.

The next is to call this method when Player is in detecting range, and using the bool to make it only be called once.

And make enemies disable attacking mode when Player is out of the range, then set the bool of detection back to false.

All set! Our enemies would be aggressive and harder to defeat.

--

--

--

A passionate gamer whose goal is to work in video game development.

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

Recommended from Medium

Designing a Testing Strategy using the Test Pyramid

Binary Tree Implementation in Java

Apache Maven : Hello World

CS371 Spring: Final Entry

Do you really know Mongodb??

Starting with the Swing App (Part 4)

The Tools that make our lives easier.

READ/DOWNLOAD%# Objects First with Java: A Practic

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
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

Day 105: Working with Animation Events in Unity

How I used scriptable objects to build a powerup system

Creating Elevator in Unity — Part 3: Moving Elevator.

Building A Stealth Game — Helping Your Guards “See”