I added context-based steering functionality to my 2d sideview free-roam shoot-'em-up game. Essentially enemy units emit raycasts eventually towards every direction, I determine interest and danger values, calculate the sum total of all vector and move the entity that way.
However, this fails somewhat quickly in the following situation:
Enemy is wandering (there is a position that is essentially its moveTarget
, which gives a high interest) around, eventually one of the (8) casts hits the player. The player is a DANGER value so the entity is supposed to move away from the player. A vector is calculated that is essentially a vector opposite the direction to the player, so indeed the entity accelerates AWAY from the player FOR A FRAME. However, next frame, by pure chance, no raycast is hitting the player, so the entity returns to its old course essentially getting close to the player (who sits along its wander target).
This happens for 10 or 20 frames, until by pure chance of the 8 casts again hits the player and we again, for a single frame, move away from the player.
How do you solve this? Just tuning up the amount of casts from 8 to 180 would work, but with 16 or 32 casts there will still be plenty of frames when the rays don't hit the player, and hence the entity sees no need to "back off".