AI: Finding Direct Path to Target

Hello UE4 Community,

Is there any way to check if a straight path from one actor to another is free? I would use the “Pathfinding Context” input for “Find Path to Actor Synchronously”, but there doesn’t seem to be any documentation about what exactly I should plug into it.

Context
The AI logic supposed to go like this:

  1. Go toward the target actor (the player in most cases)
  2. While moving, check if a straight path to the target is clear
  3. If it is, stop moving
  4. Pause for a “windup”
  5. Dash toward the target (travel along the ground a certain distance over time)
  6. Pause for a “cooldown”
  7. Repeat

The problem comes in when I want to check for collisions along this path. When using a capsule trace, it only goes in a straight line, so any time the player and enemy are on a different elevation, the collision check will not be accurate. I’d much rather use the navigation system to my advantage by simply checking if I can move in a straight line on that path without running into any collisions, but I haven’t found a way to do this.

NOTE: Even though I placed this in the Blueprint Scripting Section, I am willing to try a C++ solution. While I am by no means an expert with C++, I do have a decent amount of coding knowledge.

Thank you,
Quiteblank

The PathfindingContext pin should be okay in most basic uses without hooking anything up to it. Just give it a reference to your pawn (the thing doing the pathfinding) if it isn’t.

To tell whether you have a straight shot to the player, you can do one of two things - you can check to see if the length of the Path Points array provided by FindPath* is <=2 (Use Get Path Points on its return value to get the path as an array of points) or you can use Navigation Raycast to see if there’s a straight, navigable route between the start and end points. (Return Value will be false if there’s a direct route.)

Thank you very much! That was extremely helpful and it solved my issue. For anyone who is curious, my implementation of the first suggestion is attached.