Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by stuart6854 · Dec 28, 2014 at 08:51 PM · state-machine

[AI] Best way to Code a State Machine for Enemy

I have done some google research in different ways to code state machines such as through: Inheritance, Coroutines, Delegates. I have made basic state machines before using a combo of enums and switch statements. But i wish to make mine tidy/clean and expandable(add more states easily).

I am wondering what the best way to code one would be for a Patrolling Enemy AI which will have the following states(or more): Idle, Patrol, Attack, Investigate(lost sight of target) and Dead.

Ps. Any state machine tips are welcome as well(would love some) :)

Thanks!

Edit: I have just found out about Behaviour Tree's, i dunno if they will be relevent but it seems they are overtaking state machines? Are they a possibility?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Dec 29, 2014 at 04:42 AM 1
Share

This may be useful.

http://forum.unity3d.com/threads/implementing-finite-state-machine-ai-c.287401/

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by stuart6854 · Dec 29, 2014 at 09:53 AM

Thanks Thom and Bored Mormon for you help.

I think i will go with what i know best and use Inheritance and Classes(the way it was done in BoredMormon's link at the end) as that way seems more straight forward to me.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlbertoFdzM · Oct 25, 2017 at 09:52 AM 0
Share

Hi! You should include the links in this answer, just to help others like me looking for the info you were searching for (I would do it by myself but I cant edit yet).

avatar image
2

Answer by Thom Denick · Dec 28, 2014 at 11:04 PM

Behavior Trees are fairly complicated to code, but there are a ton of Behavior Tree solutions in the app store. Behavior Trees are better for complicated AI behavior. State Machines are just fine for an enemy that say Runs/Walks/Shoots, but if you want him to display different behaviors in different situations, Behavior Trees are better.

Here's a good Youtube video about creating a Finite State Machine in Unity: https://www.youtube.com/watch?v=lo80BOomaU4

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stuart6854 · Dec 28, 2014 at 11:11 PM 0
Share

Having a watch now, thanks.

avatar image
0

Answer by winxalex · Aug 31, 2018 at 12:11 PM

There are several way how you do it. I you have no codding experience best way is to use visual finite state machines. alt text

And when you learn the basics you can create Zombie AI with FSM. I'm codder and I'll advice you to skip all that hard codding Interfaces and stuff. In 21st you need to have everything visual which state you are, what is executed and what are the values. Switching isn't a problem. The main problem are transitions and how to break big Delagates/Events chains. Transitions should start at the end of frame of there will be glitch if transition is too long. For example you call delegate that to other state then that state call other state and chain is done. You can use Event but events in C# are multi-threaded . You can break chains with coroutines like wait end of frame or waittime. There is another problem and that is how you will sync everything with animations a.k.a Mecanim and you are split in two. I've been able to tweak Mecanim and use as ordinery FSM by use of triggers. About behaviours, behaviours doesn't take over from statemachines but they work together. Like you are in some state where some behaviour tree is executed....


screen-shot-2018-08-31-at-21526-pm.jpg (143.6 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with replays on the state machine's transitions. 0 Answers

State machine callbacks not being called on mobile iOS? 1 Answer

DIsabling a Box Collider Trigger after cut scene 2 Answers

What is the proper way to implement a 180 turn while running (Plant and turn) animation? 1 Answer

Managing GUI.depth for drawing lines in EditorWindow 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges