AI that predicts your location and shoots projectiles that way

Hello! I’m trying to figure out how to add a new attack style to my AI Enemy.
Instead of trying to explain everything, I found an image that kinda exactly explains what I would want the AI to do.


Problem is I am not at that math level, so I have no idea what I’m looking at! :confused:
I found the image from this link:

1 Like

Get the “velocity” of the target player. Multiply by the time the bullet will take to travel to the target. Then get the position of the target, add the velocity*time vector, and that’s the position you should aim at.
You can either hard-code the travel time (half a second, or whatever,) or you can in turn measure the distance between AI and player, and divide by bullet travel time, to come up with an approximate travel time.
You can also apply a differential equation to calculate the exact time of impact and exact direction, but that requires a little more math and is slightly harder to write out, so I think the above will work best for you.


Distance = Length(Target_Position - Firing_Position)
Time = Distance / Bullet_Speed
Predicted_Position = Target_Position + (Target_Velocity * Time)
1 Like

Works perfectly; thank you!

Thank you very kindly jwatte!
I am sorry that it took me this long to reply, but I had put everything with Unreal on the shelf for a while, and I just now came back to this issue and tried your suggestion. It took me a good while to figure it out, but it works in the end. I am quite scared of my enemies now lol :smiley:

The only issue I am having is that I have to hardcode the bullet speed, because I just can’t seem to figure out how to cast to my projectiles properly. Darn object is always confusing me :smiley:

Been going over this and found either a bug or I’m doing something wrong…

If I just put this math on a print and move around. If my target doesn’t move the information never updates.

Keep in mind this is multiplayer so I’m asking. The server to get my location and target information.

So essentially I have 2 actor references. Using get location on both and get velocity on target to complete the math.

If target is moving the information updates. If the player who is targeting is moving the numbers stay the same. thought?

That is exactly what the math is doing. It’s calculating the position to aim at. If the target doesn’t move, the position to aim at doesn’t move.
If you want to calculate “in what direction to aim” then you need to plug the target aim location into something like LookAt to figure out the orientation. Or just calculate TargetPosition - SourcePosition to get a vector.