NPCs


Something that struck me about the original game was this feeling of the game being "alive" with the little green men walking around.  I wanted to replicate this, and extend it a bit more to make the place feel alive again.   In the original the path that the green men took was hard-coded along some very limited and short tracks, and even then I think they were only really ever implemented in the very first parts of the game you see and not elsewhere in the map. I wanted to improve on this and get a lot more NPCs walking around across the whole map.

Unity usually has some very nice path-finding tools built in that I have used before, but frustratingly these are not compatible with their 2D engine.  Instead I've found that Aron Granberg's A* Pathfinding project is actually very good and fits in quite well with Unity 2D and so have gone with that.

My approach so far has been to hardcode in a lot of "way points" across the map, and the NPCs will pick a random way point, generate a route to that waypoint and then start walking there.  By putting waypoints at the desks of the Hotel and Bars etc, it is kinda cute to see the NPCs walking up, loitering for a moment or two then walking off again.


One problem I did hit though was that the entire map is not a continuous graph - there are various teleports that move you between the regions, and you cannot plot a single route from the far west of the map to the far east of the map in one go without going through a teleport - e.g. using the trains or even just the steps that take you under the hotel's restaurant etc.  This screwed up the pathfinding a little bit as the NPCs were picking a random waypoint that might have been in a different disconnected graph from the one they were in - this led to amusing/irritating scenarios where every NPC in the east region were all jumping onto the train tracks (that separate east & west regions) like lemmings, trying desperately to get as close as they could to a waypoint in the disconnected west region.


I had originally solved this with a hacky manual approach of tagging each NPC and waypoint with a hard-coded "region" and just manually tagging everything up in the editor which while not a great hardship felt kinda "bad" to have to do this manually.  I was in the process of writing this devlog when I thought there had to be a better way and went back to the docs for Aron's A* code and found that there was a way to find the region from a GameObject (via GraphGrids's getNodesInRegion() function) that I had missed before.  Now all the NPCs and waypoints work out which region they are in when the game starts, and the NPCs only try to go to a waypoint in the same region they are currently in.

So now we've got about 25 NPCs wandering around and generally doing things and its looking quite nice I think!

Just for the coolness factor, here is a debug view showing the walkable areas shaded in blue, and the paths for the different NPCs updating as they change their destinations - its quite satisfying to see that the NPCs are able to get right into the the little nooks and crannies of the map:


I was toying with the idea of implementing some proper goal-orientated AI (using something like GOAP) so that you could follow the NPCs around on their day - e.g. someone wants to go to the restaurant but they need money so they go to the bank first etc - but I am not sure there is enough "stuff" in the original game to really support that sort of detail: there is a bank, a police station, a hotel, some embassies and a couple of shops and that is about it.

I am not sure that really gives enough richness in the environment to really make something interesting with GOAP - there just isn't enough variety, but I might think about adding it in the future if I end up expanding things a bit - e.g. adding a new destination on the train lines.  If I do expand things like this I'll probably update the AI so that it knows how to use the trains - e.g. if it is in the west region and needs to go to the east, I can add an intermediate waypoint of the train station and then teleport them to the right destination region as needed.

Get Mechfight Remake

Leave a comment

Log in with itch.io to leave a comment.