Basically title. Have found very little online that is helpful and the few things that are are massively outdated. Would appreciate help as I’m not sure how to do it. Managed to find this code but am unsire how to use it

```
public static Vector3 CalculateInterceptCourse(Vector3 aTargetPos, Vector3 aTargetSpeed, Vector3 aInterceptorPos, float aInterceptorSpeed)
{
Vector3 targetDir = aTargetPos - aInterceptorPos;
float iSpeed2 = aInterceptorSpeed * aInterceptorSpeed;
float tSpeed2 = aTargetSpeed.sqrMagnitude;
float fDot1 = Vector3.Dot(targetDir, aTargetSpeed);
float targetDist2 = targetDir.sqrMagnitude;
float d = (fDot1 * fDot1) - targetDist2 * (tSpeed2 - iSpeed2);
if (d < 0.1f) // negative == no possible course because the interceptor isn't fast enough
return Vector3.zero;
float sqrt = Mathf.Sqrt(d);
float S1 = (-fDot1 - sqrt) / targetDist2;
float S2 = (-fDot1 + sqrt) / targetDist2;
if (S1 < 0.0001f)
{
if (S2 < 0.0001f)
return Vector3.zero;
else
return (S2) * targetDir + aTargetSpeed;
}
else if (S2 < 0.0001f)
return (S1) * targetDir + aTargetSpeed;
else if (S1 < S2)
return (S2) * targetDir + aTargetSpeed;
else
return (S1) * targetDir + aTargetSpeed;
}
```

Could you clarify what you don’t know? Like, do you need to know how to call the function, or…?

Essentially, how to call this function of that code. But also the maths in general I think? Ive been trying for hours but to no avail. I know a lot of the data I need to use but am unsure how to use it. The projectile speed, the distance between the player and the enemy, the velocity of the player etc. I tried to add the players current velocity to his position and work out the angle between the two and rotate the enemy to that angle but it didn’t seem to work. Any help would be great

I don’t get the math they’re using here, but going by the parameters, you should be able to do something like:

```
public Rigidbody2D target;
void Update() {
Rigidbody2D rb = GetComponent<Rigidbody2D>();
Vector3 theDir = CalculateInterceptCourse(target.position, target.velocity, transform.position, maxSpeed);
rb.velocity = theDir;
}
```

You probably should try and do some kind of regression. Basically meaning that you take a series of data to forecast it to a timeframe that doesn’t exist yet (I.E. the future). The point being, the AI will “forecast”/shoot where it thinks the target will be in the future based on where it previously was, and not directly at the target.

There are complicated ways to do regression, hell you could get super cool with it and create a deep learning AI that starts to learn how the player tends to move… but that’s rather novel and more in a Master’s thesis or beyond realm… What you should do right now, is what’s called a linear regression.

(if you’re not familiar) this all sounds super fancy but realistically it’s not, you can do it by always storing the last two positions of your “target” and calculating a line from it.

forecast = (y2-y1)/(x2-x1).

If you can figure out how to face the target (check out Brackey’s RPG tutorial, he goes over a technique to turn your character towards a target) then you can apply this to that. I’m sorry I can’t look for it all right now, I’m at work but I hope this gives you a direction to pursue.

Something like FaceTargetAndShoot(target.position + (forecast * time.FixedDeltaTime * projectileVelocity*distanceAway))

I’m sorry if that’s confusing and may be relatively incorrect but I hope it helps give you some food for thought

I think it’s just pure analytical geometry, judging by this line

```
float d = (fDot1 * fDot1) - targetDist2 * (tSpeed2 - iSpeed2);
```

it appears to be a quadratic determinant

the intercepting formula is a parabola, and can have two solutions in some cases.

```
float S1 = (-fDot1 - sqrt) / targetDist2;
float S2 = (-fDot1 + sqrt) / targetDist2;
```

I know this because I’ve derived it a while ago by using a homothetic center of two velocity radii. it’s an interesting technique, but in general only practical in two dimensions. [edit] I’ve just seen it’s Vector3s, so maybe it does work in 3D, I haven’t tried it myself [/edit]

the solution #2 is usually very funny because it’s more akin to planting a banana peel in front of someone, than to actively intercept its course with a bullet. this is why this algorithm opts to choose between the two.

OP, the function is pretty self-explanatory, there is nothing especially confusing about it.

You should really practice reading and writing code in general, before attempting to build anything meaningful.

Namely it obviously returns some kind of direction vector(*) from target’s and interceptor’s position and speed, where position is a point, and speed is a vector.

*I can’t tell exactly what kind of direction it is, but it probably needs to be normalized before using (and then multiplied by the interceptors speed). Or maybe it’s backwards, you should really just test it and see what it does.