Hello everyone.
Making artificial intelligence of enemies, but I have a problem with math and programming. I made several special cases, but here I got stuck.
Fights take place in turn-based mode. The enemy has restrictions on movement and the enemy cannot reach the ally in one round. How to find a place where the enemy should stop? In the picture, this is the X vector. All vectors are in the “List” in order, one after the other. The distance between AB and AX is known.
The solution is something like this. Of course, it’s better to cache distances and directions if they don’t change often.
private Vector3 GetXPosition(Vector3[] points, float targetXDist)
{
if (targetXDist < 0)
return points[0];
float currentDist = 0;
for (int i = 0; i < points.Length - 1; i++)
{
var toNextPoint = (points[i + 1] - points[i]);
var dist = toNextPoint.magnitude;
if (currentDist + dist > targetXDist)
{
return points[i] + toNextPoint.normalized * (targetXDist - currentDist);
}
currentDist += dist;
}
return points[points.Length - 1];
}
2 Likes
Thanks, very nice script.