top of page
Search
denebf33

Unity3D realization of Behavior Tree and Finite State Machine

Introduction

This is my individual final project of the course Game Artificial Intellegence. This is a third-person game that you can control a character to fight with two different AI characters controlled by different behavior trees, or you can watch them fight with each other. There is also an AI behavior tree visible indicator to show you the logic of each AI.


After you run the executable .exe file, you can choose your play mode between Player vs. AI1, Player vs. AI2, and AI vs. AI.


For the first two play modes, you can use W, A, S, D on your keyboard to move your character and use J to attack with your sword, K to attack with your shield. You can also hold Tab on your keyboard to block.


Both attacking and blocking the opponents will take some of your stamina, which is represented by the yellow slider above your character.


You can view the behavior tree and its running state of AIs by clicking the button in the upper right corner.

 

Windows Build


 

Game Logic

The actions that can be taken for any character in the game are: move, sword attack, shield attack, and block. Each character has Health and Stamina attributes, each capped at 100. Stamina recovers over time, and it recovers more slowly if the character is blocking. A sword attack costs 25 Stamina. If it hits an enemy that is not blocking, the enemy's Health is reduced by 20. If the enemy blocks, the enemy's Stamina is reduced by 10, and the character's Stamina is reduced by 10 additionally. Shield attacks cost 35 Stamina, and if it hit an enemy that is not blocking, their health is reduced by 10. If it hit an enemy that is blocking, their Stamina is reduced by 40.

 

Character Control (Finite State Machine)

In order to control the action logic and various states data of the characters, I set up a finite state machine (FSM) as the controller of game characters. The basic structure of this FSM is as follows (tweaks in the code):

The transitions in red indicate that they will be taken automatically. The AI Controller or the Player Controller can use interfaces to give commands to the FSM to take those black transitions.

 

AI controller (Behavior Tree)

I chose Behavior Tree as my AI decision-making structure. I set up two different behavior tree for two AI character, the tree structure of the first one is as follows:

And the tree structure of the second AI is as follows:

The first AI will try to attack the target when its stamina greater than 80, and if the player is blocking, it will use shield attack. When its stamina is less than 30, it will try to keep a certain distance from the target.


The second AI will also try to attack the target when its stamina greater than 80, but if it was attacked before start attacking, it will try to block. Also, after its stamina is less than 30, it will run away for few seconds then idle. At this time, when the opponent attacks, it will try to block.

 

External Assets

Unity package: Knight Warrior Mecanim Animation Pack

This package provides all the character models and animations of my project. It also provides a character controller, but instead of using it, I built my own finite state machine to control the character. The only script I referenced from this package is the camera following script, also I have made some adjustment to meet my needs.


32 views0 comments

Comments


文章: Blog2 Post
bottom of page