I recently started working on an AI for my platformer project in which the NPC moves like the player's movement itself (using qualifiers). The movement engine works in a way that only the NPC moves depending on the value (for example, if the value is 1 the npc moves to the left, and with 2 it moves to the right, and 0 makes it gradually stop moving) What bothers me is that the AI code for switching between values seems to be really messy, and I even tried programming a "checker" where, if you get too many "0's" in a row (which means the NPC will stay standing for a long time as it randomly changes the value each time), it forces the value to either 1 or 2.
And then the real problem comes around, when two or more NPCs are in the frame, they both sometimes move in the same direction but occasionally stop or shift to look in another direction, and animations seems to broke. (see attached gif)
Either way, is there an easier or just a less dummy way to program this kind of AI that just uses values to move NPCs around and doesn't use random movement for each character?