Enemy AI Orbit/Strafe Around Player

Hello, I’m working on a Top Down 2D game and I’ve started to dive into AI for the enemies. I’m having some trouble figuring out how to get an enemy to circle strafe the player relative to the direction its Looking. I currently have it so the enemy will look at the player and chase them within a certain range, but i want to add some more dynamic movement. I’m not exactly good at math, so I was wondering if anyone has any ideas on how this could work. I’ll leave some details about how im currently doing things in case it will help.

I’m moving my enemies like this :

void Update()
    {
        if (CanMove)
        {
            velocity = TargetDirection * (owner.Statistics.MoveSpeed * 2);
        }
        else
        {
            if (stopTimer.Tick(stopDuration, true))
            {
                CanMove = true;
            }
        }
    }
    void FixedUpdate()
    {
        rBody.AddForce(velocity);
    }

My AIBehaviors apply any desired movement to the TargetDirection by calling this method :

public void AddDirection(Vector2 dir, bool overwrite)
    {
        if (overwrite)
        {
            TargetDirection = dir;
        }
        else
        {
            TargetDirection = (TargetDirection + dir) * 0.5f;
        }
    }

I’m not sure if this is a great way to handle the movement direction, but so far it seems to be doing what I want it to do.

Thanks in advance for any help :slight_smile:

1 Like

Pseudocode:

if (distance_to_enemy < threshold) //threshold is distance you want circle strafing to start
{
//then we now need to move “around” the target rather than at it
movement_direction = direction_to_target + 90 degrees //counterclockwise circle strafe
}

Does that help? I can give you the actual algebra if this isn’t enough.

6 Likes

Thank you for posting, may not be your intention, but really helpful post!

1 Like

3 years after you thanked this 5 year old post, i’m thanking you lol!

Please use the Like button to show your appreciation rather than necroing threads.

Thanks.

1 Like

Excuse me lol? This post helped me YESTERDAY.
It’ll probably help someone else tomorrow, and then someone else 2 years after that.

I’ll thank OP 10x in a decade if I want, thank you very much.

The Like button exists specifically for thanking or showing appreciation for a post. You don’t need to necropost in order for a topic to remain visible/accessible (after all, you found this one!).

Pointless necroposting is against the rules (see 1i), and there are some good reasons for that. When you revive a long-dead thread just to say “Thanks!” it adds unnecessary clutter to the list of recent topics. It can also create confusion because not everyone who opens said thread will realize it’s a necropost. I’ve seen it happen numerous times where someone comes in and bumps a 10+ year old thread, and next thing you know you’ve got people quoting decade-old posts, speaking directly to the people who made those posts without even realizing how old those posts really are. It can really turn into a mess.

I’m not trying to play hall monitor or pretend I’m a moderator myself. I’m just trying to clarify why it’s a good idea to use the Like button instead of necroposting. Don’t take it personally! (And yes, I realize there’s some irony in that by making this post, I myself am contributing to the necromancy. :p)

I’m glad it helped you but when it helped you is irrelevant and isn’t the definition of a necropost. Despite the many reasons for doing it (which you included in your deleted post above) those are the rules and they are there for a reason. So you understand, the forums are here for you as long as you follow the rules so don’t take it personally, the rules apply to everyone here including myself. Look, now I’m adding to the necro too! :wink:

1 Like

Weird, you’d think by me DELETING that reply, you’d almost be able to infer that I “understood that regardless of whether or not it’s stupid, or laughable that this is something energy is being spent on over in an OFFICIAL CAPACITY, it’s not worth arguing about it, and I should leave it alone.”

And yet you went ahead and had to call it out. Almost as if…this whole thing… isnt REALLY about “uh, like, the forums are crowded.” Mysterious. I suppose we’ll never know.

As long as you understand to not necro-post and that those are the forums rules, let it be mysterious and move on.

2 Likes

Just helped me in 2025 lol