This question has been asked many times before, but most of the answers give solution to the problem where ball (target) is assumed to be moving at a constant velocity.
In my scenario, I'm making a football game. When I kick the ball into a direction by applying some force to the ball's rigidbody, I want a second AI player to find the point in the ball's path where he can intercept the ball. Now this ball can keep slowing down per frame, it does not keep moving with a constant speed.
I have attached an image for visualisation.
I know the ball's position, ball's current velocity, AI player's position, AI player's constant speed. How do I find such a point, where AI player can intercept the ball?
I am able to calculate such a point with the current velocity of the ball, but that point is not accurate, and changes as ball's current velocity changes per frame, as the ball can keep slowing down after every frame. This makes the AI player act kind of like a homing missile. Instead, I want the AI player to predict a point and just run to it.
Here's the C# (Unity) Code I have till now:
public Vector3 CalculateIntercept()
{
Vector3 pos = Ball.GetInstance().transform.position;
Vector3 dir = Ball.GetInstance().GetVelocity();
dir.y = transform.position.y;
float dist = (pos - transform.position).magnitude;
return pos + (dist / 6.17f) * dir; //6.17 is the AI player's constant speed.
}
There's no drag on the ball, but there is a physics material with friction. I suppose to solve this, I might need to account for acceleration maybe.
Please help me figure this out. Thanks!