# How to implement back and forth steering behavior?

I am trying to implement some behavior for a flying enemy in my 2D platformer.

The desired behavior is for the enemy to fly back and forth across the player, harrowing him.

If the player was standing still, the enemy would simply fly back and forth across the player, say 128 pixels to either side of him. If the player was moving, the enemy would adjust his movement accordingly. For example, if the enemy was heading left and the player was moving right and reached a sufficient distance from the enemy, the enemy would start heading to the right of the player.

I currently implement this using a simple seek steering behavior. I have the enemy seek toward a point a fixed distance to the left of the player. When he arrives at that point, I have him then seek toward a point a fixed distance to the right of the player. If the player moves, these points also move.

Further, as my enemy approaches these target points, I slow his velocity in proportion to his distance to the target. In other words, I have a seek-with-approach behavior.

There are problems with this method. For example, if the enemy is currently to the left of the player and is seeking the left target point, if the player then starts moving left, the target point will also move left and the enemy will never reach it. It will appear as though the player is simply chasing the enemy.

So my question is: How can I implement this kind of back and forth steering behavior, accounting for the fact that the target can move? Is there a simpler way that I am not seeing?

I work in Godot, but would prefer an engine-neutral answer.

• What should the enemy do instead, if it is on the player's left, seeking left, and the player is moving to the left? – congusbongus Oct 29 '18 at 0:51