# AI that predicts your location and shoots projectiles that way

**URL:** <https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608>\
**Category:** World Creation\
**Tags:** aim, unreal-engine, projectile, question, Blueprint\
**Created:** [September 9, 2015, 6:17pm UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608 "2015-09-09T18:17:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr-Manly](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr-Manly](https://forums.unrealengine.com/u/Mr-Manly)\
**Post date:** [September 9, 2015, 6:17pm UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608/1 "2015-09-09T18:17:06Z")

</div>

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.

![57937-predictiveaim.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/6/4/b641bf73ced154b0f2fb15acd237d105a5632bf9.jpeg)

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

> **[Predictive Aim Mathematics for AI Targeting](https://www.gamedeveloper.com/programming/predictive-aim-mathematics-for-ai-targeting)**
>
> A while back, I decided to look up algorithms for predictive firing on the internet, but I couldn't find any that incorporated acceleration in three dimensions,

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [September 9, 2015, 9:25pm UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608/2 "2015-09-09T21:25:20Z")

</div>

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.

Simply:

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

---

<div class="post-metadata">

**Author:** ![tundra\_cool](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tundra\_cool](https://forums.unrealengine.com/u/tundra_cool)\
**Post date:** [January 24, 2016, 11:59am UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608/3 "2016-01-24T11:59:04Z")

</div>

Works perfectly; thank you!

---

<div class="post-metadata">

**Author:** ![Mr-Manly](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr-Manly](https://forums.unrealengine.com/u/Mr-Manly)\
**Post date:** [March 9, 2016, 5:51pm UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608/4 "2016-03-09T17:51:44Z")

</div>

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 😃

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 😃

 ![81402-predicted.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/5/f/e5f8272eb78a3e7b320756421e8b87046590d37a.png)

---

<div class="post-metadata">

**Author:** ![Inph1del](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/inph1del/32/1148459_2.png) [@Inph1del](https://forums.unrealengine.com/u/Inph1del)\
**Post date:** [September 10, 2016, 6:18pm UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608/5 "2016-09-10T18:18:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [June 22, 2022, 11:05pm UTC](https://forums.unrealengine.com/t/ai-that-predicts-your-location-and-shoots-projectiles-that-way/331608/6 "2022-06-22T23:05:13Z")

</div>

> [@Inph1del](#):
>
> If my target doesn’t move the information never updates

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.
