How do I randomize the location where an AI is chasing the player?

How do I randomize the location where an AI is chasing the player? So, when multiple AIs try to chase the player, They don’t move in a straight line. Here is my code so far:

Hey @KillerthePro756!

To help get things started, can you go into further detail about the problem you are facing as well as provide more specifics on the end result you are trying to achieve? Are you trying to randomize the route your AI is taking, the endpoint, or something else?

Any additional specifics you provide may go a long way in solving your problem?

Sure, I have no problem with the chasing. The thing is when all of the same AIs all try to chase the player they all bunch up and they move in a straight, unrealistic line which looks very cheap and of course unrealistic. And i am trying to make them to go around each other which is a bit more realistic. And I am trying to randomize the route my AI is taking to get to the player. And that’s it

Perhaps it’s a matter of creating an offset vector for each AI to chase. It would be similar to your method. Rather than chasing the player, they’d chase a coordinate next to the player and, most importantly, ahead of it. You could derive the latter from player’s velocity.

That could work, But I don’t know how that would work since i barely know how vector math works.

You can get Forward / Right vectors, negate them to get the opposite directions, multiply it by distance to get an offset in that direction. Pawns generate velocity, it can be added on top. This would produce 4 locations around the player while accounting for their velocity, giving the ai a chance to intercept a location where the player will be, a lead:

AIs chase the green points instead of the player.

This is just pseudoscript, can be made much better, have values massaged to serve your needs, ofc.

thats actually tricky, even if you offset the end location, the path locations would likely still overlap.

id handle this differently and have the AIs ‘push’ each other away so they naturally spread out. its kinda how starcraft 2 does it

What you really want is to “push” the trajectory of the pathfinding path.
This might be a neat feature to have in the base pathfinding system, TBH, but currently, that’s not available.
One way to fake it is to artificially inflate the radius of some of the chasers, so they will extend out further from corners. Of course, if there are narrow doorways, then that artificially wider agent wouldn’t fit through, so that’s not perfect, either.

That would work, But then it would look weird because if the player stops the AIs will gather around in a circle in front of the player. And the AI is supposed to interact with the player.

It was mostly to answer the question about vectors, but you would obviously add more script to handle what happens when the AI catches up… Once they’re close enough, switch to another subroutine. Note that you have the entire:

To explore.

So, for example get the locations and make them closer to the player.

How far the points are is dictated by this single variable, lower the value as soon as the AI is ready to engage:

image

You can also disregard player’s velocity once the AI is close enough. Essentially this would be just for catching up with a bit of scatter, then you’d script another appropriate behaviour.

Ah, Got it now i will test this, And i will see whether it works or not.

After I was done with the code I tested it and it didn’t work it just fails here is the code:


Keep in mind I am doing all of this in a BP Task.

I mean, this would not work well.

  • you’d need to send each AI a different coordinate. Here you’re sending only the last one
  • you also entirely skipped player world coordinate and are treating the local offsets as world. You’re sending values -30 ↔ 30. Add player world coord at least. And send a unique coord to each AI instead.

This is just pseudoscript

It was just an example of how to calculate some vectors, not something you could possibly plug into an existing system. To make it work with elevation, you’d also need to incorporate some kind of downwards tracing and obtain the location from that. Not to mention narrow passageways that would wreck havoc with this and would need to be be handled as a special case.

Essentially, you’d script a bunch of routines and switch between them:

  • follow in line
  • surround player
  • surround but keep distance
  • and so on…

You’d also need to breathe some logic into handling the coordinates themselves - what happens when there’s 17 AIs and only enough space for 12 AIs to chase the player. You may also look into:

if you have not done so already.

you also need to validate the vector since youre moved it, ie is it on the navmesh and not blocked.

EQS would be good for this

1 Like