September 23, 2015
.
.
Hi everybody!
It’s time for a closer look on the fauna of Mold, more precisely on the micro brains of all those little (gladly pretty stupid ;) ) critters, which populate the environment.
The hole idea of having free acting / AI (artificial intelligence) controlled insects in the game came when I bought playmaker for Unity :) , which adds finite state machines (FSM) to the mix. I originally planned to simply move a few insects on predefined paths through the sets, but with this addition, even I had a chance to get something more dynamic going ;)
Before we start, let me ‘state’ that i am by no means an expert on FSMs or AI so take my advice or setups with a big grain of salt. I’m basically just reporting how I did it, and how i got it to work, not how a ‘FSM/AI pro’ would probably do it. (so in case someone notices a big no-no, please don’t hesitate to contact and correct me.)
… but it works and by my understanding everything was done as it should be. (see? apply salt here ;) )
.
Ants (approx. IQ: 0,05 points)
BUGS!! BUUUUGS !!!!!!!!!!
.
I created the first version of this FSM for the third Mold prototype (v0.03.xx), which, granted, did the job, but had some major (but not very obvious) problems. (like sporadically vanishing ants) The entire setup (still) relies on Unity’s pathfinding, but some parts of the FSM (like setting the agents goal) ran incorrectly on ‘Update()’ (means: every single frame), which probably caused this behavior.
I revisited this AI after i (learned a lot from and) finished the fly, and fixed most of its problems as far I can tell.
.
The Unity hierarchy looks like this:
[ANT_prefab( FSM1 )] -> [Sprite( FSM2 )] -> [view collider( FSM3 )]
A simple cone/cylinder (acting as trigger), follows the ant and “looks” for the player (it fakes the insects point of view). This event is fed into FSM1, which resides on the main prefab and controls the overall movement and behavior. The view cone itself is randomly (and in steps) rotated around its Y axis, which causes some of the insect-like, erratic movement. FSM2 finally just aligns the sprite to the active camera.
Finally, a forth FSM instantiates the ants, modulates their speed, and registers/add them to the AdventureCreator engine as NPCs.
.
Fly-o-poop (approx. IQ: 0,07 points)
.
.
Unfortunately this (flying) creature needed a completely different approach, so I started from scratch and used the opportunity to improve on my old design. (which is usually pretty easy if your last design was also the first ever :D )
This time i couldn’t use pathfinding (well, it flies), so I had to get my hands dirty(er than usual) and transform it manually (basically creating my own tweening inside playmaker)
.
Unitys hierarchy looks like this:
[FLY_prefab( FSM1+FSM2 )] -> [noise( FSM3 )] -> [Sprite( FSM4 )]
[FLY_lookAt]
This NPC has 4 basic states. 1. Fly from start to goal, 2. follow player, 3. annoy player, and 4. fly home. This time I didn’t use a trigger to detect the player, I simply check the distance between player and fly. FSM2 is just there to generate all global perlin noise vectors (+distance calcs) which get fed into the other FSMs/states when needed.
.
.
A big improvement compared to my ant AI is that all states transition smoothly (which is not as trivial as you might think ;) at least for suckers like me), the noise FSM(3) applies additional random transforms to the fly without further complicating the main FSM. (in my opinion it’s easier to use Unitys hierarchy instead of trying to calculate every motion or offset in one state)
The Flies are aligned to the FLY_lookAt gameObjects (FSM4), which are moved between the view targets by the main FSM(1) (yellow lines in the animated gif). (the green lines show the camera alignment, the colored label of each fly shows the current state)
that’s all I can remember for this time ;) , have a good one!
–s
.