Search Unity

What is best way to implement ai?

Discussion in 'Scripting' started by Roldo, Jun 25, 2019.

  1. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    41
    Currently I am working on AI system for my single player RPG game, however i cant choose what type of AI to use. Id rather have a decision based system then a hard coded logic. What do you think is the best type of AI to use? Which is the most comfortable to work with?.
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Behavior trees are quite good and a standard across the industry. There isn't really a "best" way to do AI in general, otherwise Unity would just ship that solution.
     
    ericbegue likes this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Unity might actually be taking a stance on this, maybe? There's now an AI Planner package in the Package Manager:

    https://docs.unity3d.com/Packages/com.unity.ai.planner@0.0/manual/index.html

    It's extremely new, and probably not something to seriously consider using just yet, but it's interesting that maybe in the not-so-distant future, Unity will have a somewhat built-in solution.


    For now, though, there are a lot of options. One that I've used was outlined in this tutorial:

    https://learn.unity.com/tutorial/5c515373edbc2a001fd5c79d

    It's not a visual solution (no pretty diagrams showing state transitions, like you'll see on some asset store offerings), but it's pretty simple to get it working and deal with simple AIs.
     
    sas67uss and WallaceT_MFM like this.
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Oh wow, I don't know how I missed this. Thanks for the info. Guess I was too distracted with the ML developments to notice haha.
     
  5. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    41
    Thank you, ill definately check out your tutotial. I have considered using Unity's Ai Planner, however i had some problems with it. For example it requires Unity's ecs system which is not quite ready aither, and it also has a few other issues like incompatibility with newest ecs preview and others. Ive also considered using other GOAP frameworks like ReGoap, but they are undocumented and are not that comfortable to work with.
    Is it better to use GOAP or a similar system or something like decision trees?
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    GOAP sounds much more sophisticated and smart, due to its ability to evaluate multiple possible options, and choose the best one. I just don't have any first-hand experience on whether it's more/less complicated to implement, compared to the one I linked. By the way, the one I linked is basically just an implementation of a finite state machine. So you could first evaluate the characteristics of finite state machines compared to GOAPs, and decide whether either, both, or neither, suit your needs.

    Also consider that despite GOAP seeming more powerful, you should be sure you have a use for that power. If your AI is relatively simple (A soldier patrolling an area, shooting any player who gets close, returning to patrolling if the player runs away) then probably any system would be fine.
     
  7. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    41
    Yea, youre right, GOAP would probably be an overkill for what i need, thanks for help)
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I suggest using an enum of states such as idle, moveto etc and use a switch statement to decide what the AI should be doing based on that enum's value.

    This is the most straightforward AI ever, and my games use this approach. GOAP and behaviour trees and all that will, at this point in time and this point in Unity's development, severely hold you back from progress.

    The reason for this is because none of it is remotely finished on Unity's side and anything in asset store will just drag you under as you try to learn that instead of finishing what most likely will be perfectly handled by a basic state based AI.

    I assume this because if you don't know anything about AI and (if you do and you're an experienced single developer) then you will know a state based AI (regular code) will be best.

    For teams that can't touch actual engine code then a behaviour tree or GOAP is the way forward but that's not your typical indie.

    Please don't get sidetracked with compelling noise from the industry, half the advice isn't worth a damn unless you're in an AAA studio which it's usually parroted from, for a completely different workflow, for a completely locked down engine in a company with strict policies on touching source code.

    So if you don't know how to do even trivial AI with regular code you're basically going to get screwed with GOAP and Behaviour trees because those are designed for very specific setups which absolutely NOBODY ever mentions (except me maybe).

    Imagine if your game could achieve as much as a simple world of warcraft npc could? That would be amazing. Except WoW doesn't use behaviour trees or anything of the sort, it uses very, very simple logic, very straightforward.

    None of the stuff outside of GOAP + machine learning is actually AI, it's just different ways of doing a state machine with an impressive name attached (would you really call WoW's AI "artificial intelligence" ?)

    And if an indie is aiming higher than blizzard then I don't even, TBH.


    TLDR:

    Recommend a switch statement working with an enum of easy to read values such as:
    public enum State {None, Idle, MoveTo, Attack, Follow, Dialogue} and so on...
    public State state;

    switch (state)
    ...

    So you can control the flow in an easy to read manner within the program and apply your usual practical programming experience. Really, most people just miss the enum switch stuff. This is so simple, its the program flow and the core of what people call a "state machine" which really isn't that impressive (much like AI sounds more impressive than it is).

    If only people cut the crap, then more games would ship. You really can't keep applying AAA development practises for a large studios to individuals. It's why nobody ships.

    Course for teams, the answers do change.
     
    Emolk, Roman200333, WayneJP and 2 others like this.
  9. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    41
    Thanks for such a detailed reply) Of course i am not aiming above blizzard) And I aggree that GOAP would be an overkill. I will use a state machine for my game, i was just considering possibilities)
     
  10. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    @hippocoder I agree with parts of that and disagree with others. I assumed that OP was already familiar with regular script based AI and wanted to try more advanced systems. Behavior trees and state machines are pretty much the same thing, but behavior trees have more implicit transitions, which is good or bad depending on context. I'm not sure what you mean when you say they are only for AAA studios or teams; I use them on my own and ship games just fine. Also, what specific setup are you talking about? Do you mean their visualization or something? AFAIK, you can use whatever behavior you want, for any kind of agent.
    Let's be honest: the WoW AI isn't good, and it isn't supposed to be. That's not an insult against Blizzard or WoW, it's just their design choice. If you don't need anything more than that, it makes sense not to use anything (unless you just want to have fun making the system). However, if you want a stronger illusion of intelligence out of your agents, a single file state machine will start to get unwieldy very fast.
    I suppose this comes down to what sort of game you want to make and what is important to it/you. Do you only need your NPCs and monsters to stand around, and occasionally attack something? Basic state machine. Do you want your agents to have many complicated systems and/or work together on multiple levels? Probably want something more organized.
     
    WayneJP likes this.
  11. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Interesting. You seem to be making a differentiation there where until now I thought to be none:

    „Decision based system vs. hard coded logic“ - aren’t both hard coded?

    I think the base for your decision should be what your game needs. High-caliber AI is usually not required for most action-oriented games. If you are going for chess or smart dialogues, perhaps („Eliza“, anyone).

    I have used Behaviour Trees, FSM and bespoke code. Nine times outer of ten, the bespoke code was superior because it did exactly what that NPC was required to do, took much less code to develop, was easier to maintain, and better performant. Few people realize until they try themselves, but most monster NPC only require a small, almost generic set of states, and once you develop these behaviors (don‘t you love OOP?) in C#, you have a solid foundation to pick and choose from to build 99% of your mobs and NPC.

    So go with c# first, and only move on to other systems once you understand how c# states work. You‘ll quickly realize that (especially behaviour trees) are pretty much ‚node-varnish‘ to glue together your C# code — because my behavior trees for AI consist of >80% self-written nodes (I‘m using NodeCanvas).

    -ch
     
  12. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
    Very good thread
    Hi friends
    What is the best solution for implementing shooter AI for mobile with limited and simple behaviors but with beautiful details?, like enemies in "Call of Duty" , of course considering performance.