Need Help with Ranged Enemy AI Getting Stuck While Running Away (Need Help)

I didn’t check out the existing code you provided, but you can achieve your desired behavior by casting line traces from your enemy AI to a little bit further away. To do this, you can get a For Loop with Break node and insert your desired values into the First and Last index pins. Though it would be convenient to have 0 as the first and 90 as the last index. After the Loop Body execution pin, place a Line Trace by Channel node. Then get your enemy AI’s location by using the Get Actor Location node and plug it into the Start pin of the line trace node. After that, get the AI’s forward vector, multiply it by something like 500, add the AI’s location onto the result of the multiplication, and plug the result of the addition into a Rotate Vector node. Split the rotation pin of that node by right clicking on it’s purplish-grey pin and clicking on the Split Struct Pin option. Now drag the index pin of the For Loop with Break node and plug it into the exposed z axis of the Rotate Vector node. Once that’s done, you can plug the yellow Return Value pin of the Rotate Vector node into the End pin of the line trace node. Then, place a Branch node after the execution pin of the line trace node and plug the Return Value pin of the line trace node into the Condition pin of the Branch node. And finally plug the True execution pin of the Branch node into the Break execution pin of the For Loop with Break node.

Now, after the Completed execution pin of the For Loop with Break node, you can get that final result we got earlier to determine the end point of our line traces, convert it to a unit vector by plugging it into a Normalize node, and use it’s result to set a vector type variable. This variable will be the direction that your AI should be moving to.

You can also implement the same thing for the left side by calling the same function, but just switch the Last Index pin of the For Loop with Break node from 90 to -90. You can call them one after another before determining how should the AI keep moving, compare the last index values of the right and left sides (not the fixed last index value that’s inserted into the Last index pin, but rather the dynamic one which you can access from the Index pin) and choose the result that was in the loop with fewer iterations.

About when to call these functions, you can add a collision volume (box, sphere, etc.) that’s gonna be located accordingly with the end point of our line traces (in this example it was 500 units further away from the character) and call the functions when it overlaps a static mesh.

I hope my descriptions were clear, and hopefully they can be helpful! :innocent:

1 Like