Hello guys,
I’d like to discuss what you find out to be the best way of implementing enemy patterns and behaviors.
I’ve been thinking a lot about making the system modular and flexible.
1- The first piece of my puzzle would be a state machine, I thought I could use Mecanim and its transitions system to create the “pattern/combo” aspect of it. I think state machine behaviors could be use to link each “state” (aka step of the pattern/combo) to the actual enemy attacks and stuff.
2 - to make it modular, I think that each State of the combo should be as generic as possible like “quickAttack1” → “dodge” ->“powerAttack1”
3 - these generic states must link to an actual action, different depending on the enemy (“quickAttack1” can be a punch for a ninja but an arrow for an archer)
You guys can point any flaws in the system already but now I have to ask myself (and you ^^) how to actually implement that ?
4 - How to actually manage this layer of abstraction ? I’ve been thinking about ScriptableObjects but without really figuring it out.
Thank you guys for all you input and ideas, let’s make it a nice debate !
I would not rely my enemy ai on the animations. It’s a part of visualization, i.e. kinda program output. Messing input and output may lead to weird logic loops and unpredictable behaviours. I would create a character class or prefab from multiple classes called character and two scripts for player controller and ai controller and split enemy behaviours so the alghoritm is within ai controller and settings for algorithm are stored in a scriptable object.
Maybe I wasn’t very explicit but I wouldn’t use the Animator for graphics animations, I would only use it as a SMB that triggers actual behaviors.
I haven’t been thinking to much of the animations already, I guess I would deal with them by code, triggered by the SMB just the same as the actual data/behavior part.
Does this make sense ?
I totally agree with this, and here is why: relying on a back-and-forth system where some of the AI is in code and some of it gets triggered by events at various places in the animation can make debugging quite tricky, even if it makes timing a little bit easier.
Not only that but relying on animation makes a hidden dependency that your artist and/or animator may not be aware of, especially far in the future. Even if YOU are the animator/artist, if you dig this out a year from now and change the animation and break it, you will be hard-pressed to remember WTF.
I recommend doing all your AI in code, definitely with data assets that supply things like curves and positions, and then send over animation variable changes from code to the animation.
That makes total sense and I might miss something but the way I see it, using Mecanim as a visuel SMB that triggers actual code let me with a code decoupled from the animation part as the animator doesn’t actually triggers animations but just set States.
If both of you guys think I’m wrong I guess I am but I’m kinda confused here ^^
When you say data assets we are talking about SO here, right ?
Sure, scriptableobjects, prefabs, scenes, whatever. I am personally a HUGE fan of Unity’s scene system. For instance if I was making a space shooter game with lots of different levels, I would put each level into its own scene and load it additively, then when all the enemies are killed off, unload the scene and load the next one additively.
This lets you be editing the scene, press PLAY and see how the enemies behave, lather, rinse repeat. You can still have global configuration state such as “current difficulty” that drives the speeds of the enemies or their shots, or how often they shoot.
I break all my games into lots of small scenes, each with a single point of concern:
player controller
gameplay ui
pause menu (loaded when needed)
content scenes (level after level)
I like to unload my player controller scene when you die, then reload it and have him respawn appropriately.
Scene control like that in Unity is one of the most amazingly powerful tools, ESPECIALLY when you’re working on a big team, but even when you’re working alone. It enables vast reuse of assets and code in a way that few other systems allow.
I see your point but animator states are hard to rely on. For instance, it triggers next state enter before current state exit under certain conditions, it may skip events, and so on. It is designed with animations in mind. If you want visual fsm representation you’d better look for some asset for this. There are a lot in the asset store.
Otherwise, having separate fsm is good idea. You can have common character ai script and attach different fsms stored as assets just as animator + animator controller works
Wow I’ve never considered scenes this way and I’ll have to re-read your post again and slowly !
Right now I have to go but will be back tomorrow, thank you for the good talk
About the SMB part, are you still thinking this would be a bad approach ?
It takes a tiny bit of planning but it’s still very flexible and …
… once you separate all concerns and break things down by scene, you’ll never want to go back! Every other project you see will feel like a morass of junk all crammed into a big box. It’s glorious to use scenes this way.
The good news is that you can usually start retrofitting existing games immediately. For instance, take the UI portions and make them their own scene. I usually do this by cloning the scene they were originally in, then deleting the parts of each scene that don’t belong in the other. When I’m retrofitting I usually only break ONE thing out at a time, and I always use source control 100% of the time so if I screw something up, going back is trivially easy.
I’ll take a moment to plug source control once again: use git, it’s AWESOME in a Unity context and there are tons of online tutorials. Once you start source controlling your projects, you’ll never want to work without it again.
I’d like to know yout thoughts on the new subscenes feature what exists already in 2020 alpha? Particulary Im thinking if I should try it as you said as completely separate scene for character or break my nex game into subscenes?