Incorrect route calculation

Hello, I am having two fundamental problems to be able to approve my project.
The first problem is based on the calculation of the route, when I move an agent towards a destination in front of him or behind him, the agent should move in a straight line, however, the agent makes a small unwanted curve.

The second problem happens to me when rotating the agent in a custom way (RotateBackward), when I am going to force the movement to a new destination immediately I perform the following code:

    this.MiCustomAILerp.canSearch = false;
    this.MiCustomAILerp.TurnRate= true;
    this.MiAIDestinationSetter.target.position = destino;
    this.MiCustomAILerp.destination = destino;
    this.MiCustomAILerp.SearchPath();
    this.MiCustomAILerp.canSearch = true;
    this.MiCustomAILerp.MoveToPosition();

public void MoveToPosition()
{
if (TurnRate)
{
this.enableRotation = false;
this.updateRotation = false;
Vector3 pos;
if (!interpolator.valid)
pos= simulatedPosition;
else
pos= interpolator.tangent;
if (RotateBackward(pos))
TurnRate= false;
else
FinalizeMovement(tr.position, tr.rotation);
}
if (!TurnRate)
{
this.enableRotation = true;
this.updateRotation = true;
Vector3 nextPosition;
Quaternion nextRotation;
MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
FinalizeMovement(nextPosition, nextRotation);
}
}

public bool RotateBackward(Vector3 posOrigen)
{
Vector3 vDirection = (posOrigen - transform.position);
float vAngleResult = Vector2.Angle(new Vector2(transform.forward.x, transform.forward.z), new Vector2(vDirection.x, vDirection.z));
if (vAngleResult <= 11.5f)
return true;

    vDirection = posOrigen - transform.position;
    Quaternion vRotation = Quaternion.LookRotation(vDirection);

    vRotation = Quaternion.RotateTowards(transform.rotation, vRotation, 50 * Time.deltaTime);
    vRotation.x = transform.rotation.x;
    vRotation.z = transform.rotation.z;
    transform.rotation = vRotation;
    vDirection = (posOrigen - transform.position);
    vAngleResult = Vector2.Angle(new Vector2(transform.forward.x, transform.forward.z), new Vector2(vDirection.x, vDirection.z));  
    return vAngleResult <= 11.5f;
}

This sometimes works fine and sometimes it doesn’t, sometimes it rotates immediately and I think it is by the calculated path since simulatedPosition or interpolator.tangent is not correct sometimes.

What am I doing wrong? I use AiLerp with simple smooth modifier (simple smooth type, uniform length = true, max segment length 0.2, iterations 2, streangth 0.5) on a grid graph with Euclidean herustics, connections eight, nodes width 640 nodes depth 640, size of node 0.25, center 80,0,80

In this video you can see how these two problems arise:

Looking through the forum I saw that this solves the problem of the turn, although sometimes he does not get the position correctly, in this way it obtains the destination starting point of the new route:

Vector3 Lookahead (float distance) {
var originalDistance = interpolator.remainingDistance;
interpolator.remainingDistance = originalDistance - distance;
Vector3 result = interpolator.position;
interpolator.remainingDistance = originalDistance;
return result;
}

I need to know why does this route calculation so strange? there is no way the route is totally straight.I’ve tried everything and I don’t know what to do anymore.

I’ve been looking at the forum more and seeing this link:


can this be the solution, even though my map is not rotated? I have tried to modify it but since the script belongs to the package folder I cannot modify it

Hi

It’s hard to see in your video since you do not show the actual path using gizmos. However from the movement I think you are using a grid graph, and the movement is pretty much following the raw tiles.
I’d recommend that you try the Raycast Modifier. It’s very useful on grid graphs to simplify the path to make it straighter. In this case it should be able to simplify the path to a straight line.

See also https://arongranberg.com/astar/docs/raycastmodifier.html

1 Like

Nice! this finally solves the first problem! I had the euclidean heuristic type and I changed it to manhatan and added the raycastmoddifer component and it works perfectly! Thank you so much.
Finally, I still have to solve the second problem.
With this last code it works correctly most of the time but there are times that if I am making a route and before reaching the destination I change my route, the position of the first node obtained from the new route arrives incorrectly and does not make the turn.

New code:

public void MoveToPosition()
{
if (TurnRate)
{
Vector3 direction;
GraphNode vGrapghNode = NextNode();
if (vGrapghNode != null)
direction = (Vector3)vGrapghNode.position;
else
direction = Lookahead(0.5f);
if (RotateBackward(pos))
TurnRate = false;
else
FinalizeMovement(tr.position, tr.rotation);
}
if (!TurnRate)
{
this.enableRotation = true;
this.updateRotation = true;
Vector3 nextPosition;
Quaternion nextRotation;
MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
FinalizeMovement(nextPosition, nextRotation);
}
}

public Vector3 Lookahead(float distance)
{
var originalDistance = interpolator.remainingDistance;
interpolator.remainingDistance = originalDistance - distance;
Vector3 result = interpolator.position;
interpolator.remainingDistance = originalDistance;
return result;
}

public GraphNode NextNode()
{
if (this.path.path.Count > interpolator.segmentIndex + 1)
return this.path.path[interpolator.segmentIndex + 1];
else
return null;
}