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: