This project is a modular state machine system designed for managing AI behaviors in the Unity game engine. It includes various AI states (e.g., Idle, Attack, SearchForTarget, TakeRange) and control mechanisms (e.g., Movement, Attack, Perception) to create flexible and reusable AI behavior systems for game developers.
- Built-in States: Defines different states for AI entities (Idle, Attack, SearchForTarget, TakeRange).
- Control Systems: Manages AI behaviors with controllers for Movement, Attack, and Perception.
- Easy Integration: Easily integrable and customizable within Unity projects.
- Flexible Architecture: Allows adding new states and controllers with minimal effort.
- Modular States: change the states in the inspector without writhing any code
The project's scripts file structure is as follows:
📁Scripts
├── Entity.cs
├── PlayerController.cs
│ 📁StateMachine
│ ├── AIController.cs
│ ├── State.cs
│ ├── StateMachine.cs
│ │ 📁AIStates
│ │ ├── AIState_Attack.cs
│ │ ├── AIState_Idle.cs
│ │ ├── AIState_SearchForTarget.cs
│ │ ├── AIState_TakeRange.cs
│ │ 📁Controllers
│ │ ├── AIController_Attack.cs
│ │ ├── AIController_Movement.cs
│ │ ├── AIController_Perception.cs
-
Unity Version: The project has been tested with Unity 2021.3.
-
Clone the Repository:
https://github.com/yunnsbz/Unity-State-Machine.git
-
Open in Unity: Open the cloned project using Unity Hub.
-
Set Up the Scene:
- Create an enemy entity (GameObject): add the Entity, AIController and a NavMeshAgent component.
- set the layer to
Enemy
. - Add controllers (Movement, Perception, Attack) and connect them to the AIController.
- set target (
Player
) and non-target (Default, Enemy
) layers for the perception controller. - Create a player entity (GameObject): add the Entity and the PlayerController scripts.
- set the layer to
Player
.
-
Configure States: Customize AI behaviors using the states defined in the AIStates folder.
- Run the game. Use
W A S D
keys to control player character and observe the enemy AI behavior.
If you'd like to contribute to this project:
-
Fork the repository.
-
Create a new branch for your feature or bug fix.
-
Make your changes and submit a pull request.
This project is licensed under the MIT LICENSE. See the LICENSE file for details.