Videos

Unity3D AI with State Machine (FSM), Drones, and Lasers!



Jason Weimann

Join my Game Architecture Course Today and get free bonuses! – https://bit.ly/2WIyE4v

——-

Create a Unity3D AI system using the State Pattern and State Machines. We’ll build an FSM (finite state machine) in c#, make it generic, reusable, and able to drive our AI bots.

State machines and AI are extremely important in game dev, so we’ll start with a simple switch based AI system then build it into a solid c# FSM that we can extend and build upon.

And if you’d like to follow along, all the c# source code for this unity tutorial is available below.

Download the code for this Unity3D tutorial here: https://unity3d.college/2019/04/28/unity3d-ai-with-state-machine-drones-and-lasers/

Join the group: http://unity3d.group

Patreon: https://patreon.com/unity3dcollege

Source

Similar Posts

44 thoughts on “Unity3D AI with State Machine (FSM), Drones, and Lasers!
  1. another great Video, learning so much, But quick Question, in WanderState script , line 85 private Transform CheckForAggro(), is there anything after line 104, since getting error not all code paths return a value. Tried to download project but only shows first AI in video for 2 bots

  2. Great video. I've been struggling with State Machines and saw it as 'too hard, I'll do it later' type of thing, but your example and descriptions are really good. Will you be uploading the final scripts to your website?

  3. Welp, I've transposed all the code and Unity hangs when I try to run it.

    Also, while this is clearly a working setup (as shown in the video) I've noticed the variable names in BaseState are the same as their permanent/generic names in Unity.

    So…

    protected GameObject gameObject;
    protected Transform transform;

    To my (limited) understanding this is bad practice as it leads to confusion, if not with Unity, then with other users (right here =); especially as we can name our variables whatever we want, why name them the same as the generic references?

    Anyway, I'm gonna try and get this to at least run and then I'll post the scripts somewhere. If anyone can give a suggestion why it's hanging that'd be grand =)

  4. Hi Jason –

    Yet another great tutorial, as usual, and definitely helped me a lot in breaking my own monster FSM Update() class down into something a bit more manageable. One question: I noticed that you store variables related to each state within the state (for example, the _attackReadyTimer variable). How would you handle a case where you needed to show this information on the screen (e.g., an indicator showing how much time is left until the drone is ready to attack again?)

    One way to handle it would be to put the _attackReadyTimer variable into the Drone.cs class, but that could potentially lead to bloating the "main" class with variables specific to each state, thereby reintroducing a lot of the complexity you wanted to avoid in the first place.

  5. Here's my version of the scripts in the above video. +Unity3D College let me know if you want me to take this down ๐Ÿ˜‰

    I've changed some of the variable/class names to suit my purposes and also, as my game is 2D, I've modified the movement from transform.forward to transform.up. Without being rude, if you're looking at this level of scripting you should be able to figure what equates to what. That being said EnemyStateMachine, Drone and IndividualAISettings all go on your drone/enemy. I've also included a couple of new states that I need, Retreat and Escape. You'll probably need those in your project even if you're not using them or you'll have to modify the dictionary. Anyway, hope this helps.

    https://www.dropbox.com/sh/de47ghhhrd8r6a0/AAAfumYQYgCr5C6GPxKBTamwa?dl=0

  6. Awesome lecture man. Thank you so much for helping me understand this concept. Need this for my own use and glad I get the chance to understand the concepts and not just rewriting someone else's code.

  7. This line:

    private readonly LayerMask _layerMask = LayerMask.NameToLayer("Walls");

    …is not working for me. If I change this to:

    int _layerMask = 1<< 8;

    …then it's all fine. So basically the above (first) line is not pulling back what it should be, and the bools all return "false" when they should return "true".

  8. So I managed to make it so my states have modular conditions. The conditions are inherited objects, each have an abstract bool called "EvaluateCondition()." If the condition returns true, then the current state will transition to the next state. I can add as many conditions as I like before firing the actions. I can also set it so All, Any, or None of the conditions must be met before firing the action. I simply drag and drop the conditions into each state's condition properties, modify the conditions, and set the action. I'm thinking about adding this to a library name space and building it into Unity itself so my finite state machine acts as a real component.

  9. ์™€… ๊ธฐ๊ฐ€๋ง‰ํžˆ๊ฒŒ ์ข‹์€ ๊ฐ•์˜๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ˜€

  10. This is a great video, but it would be infinitely easier to follow if you didn't include all the drone code and just showed changing of states at the press of a button.

  11. Very nice. Like this a lot. I think I would prefer an enum of some type for the states though, this is the kind of thing where I am CONSTANTLY forgetting what I named stuff.

  12. Thank you Jason. This is amazing stuff.

    I've always wanted to know HOW gameplay programmers made those NPC/CPU characters to 'think', and to fight against us – human players (I have wondered that since I was 6 years old, playing 'Beat 'Em All' games (e.g.: Brawl Brothers, Final Fight I, II, III, etc.) because I was amazed by the complexity of the characters' behaviour in some cases).

    It is funny somehow that it can be accomplished by solution so simple as: a SWITH – CASE STATEMENT(or some IF – ELSE IF – ELSEs) with a ENUM Variable for all the possible Enemies' States.

    Please, don't hesitate to make more in-depth videos about State Machines, A.I., and Machine Learning Tips and Tricks. Your videos are really great.

  13. Really love the advanced stuff you're covering and this seems really cool but I just have 1 gripe with it. Each of your states has knowledge of the other state. I think I'd prefer extracting out the decision making into a Chain of Responsibility that gets called in Update and just have the state perform the action that it is instructed to do.
    This way your states are decoupled, so is the decision making and this would then facilitate pulling this data from a datasource.
    Cool stuff any way

  14. Jason, as always thank you for the awesome guidance. I am interested in diving into AI more and like your previous way of coding AI states I too did the same thing i find it hard to enhance down the road. What I am looking for help with is a more modern day A.I. where the entity isn't so much patrolling or wandering, but guarding an area or zone until acted up on. Then it does the normal chase/hunt, attack/destroy. What I don't see a lot of included is: Flee, Call for Support, and destroying potential places a player might be hiding. The latter I would like to see how you incorporate into your A.I. Much appreciated and as always – thank you.

  15. im totally lost with this, trying to just do things while in a state. trying to use another script to change the players sprites. I keep getting null reference exception and yet the code from the other script will still run. Im just not sure of the proper way to reference other scripts and use them in state. I want to controls player animations with the states.

  16. Jason Weimann The download link seems to just have one file in still?
    I am in the process of building the scripts from the video but would love to see the originals to compare how I did ๐Ÿ™‚ Thanks

  17. 20:06 _availableStates.Values.First() will blow up if there are no states in collection. Question marks below are extra operations. FirstOrDefault will return null if state is reference type

  18. can anyone help me figure out the the Type thing is suppose to be at around 15:40?

    ive recreated the scriptes as best as i can but this just shows as an error in the editor and its never gone over in the video

  19. So upon, switch to new state void, we can check which state and crossfade an animator on a character. I think it's a great and optimized way to have re-use of these scripts for enemy ai.

Comments are closed.

WP2Social Auto Publish Powered By : XYZScripts.com