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

Closures in Swift, SwiftUI

Developer versus QA Productivity

5 Simple Steps to build a Full Stack Spring Boot Web App from scratch

Windows Users : How to build RYO from source on Linux using WSL

Kotlin DSL | Coding a DSL: 3 — The ‘Triangle’ and ‘Rhombus’ objects

Open-source Machine Learning Database OpenMLDB v0 4.0 Product Introduction

What’s new in Java EE 8(by example)

Golang in 5 minutes

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

How to Make the Camera Shake Using Impulse Listener Extensions in Cinemachines Virtual Cameras

Adding a thruster speed boost on keypress

Creating a Loot System (part 2)

Creating a Simple Cooldown System In Unity