Hello I am having problems receiving the initial position of the new route to be calculated, I need the initial position to be able to make a rotation towards that new destination, here is a video where you can see that sometimes it turns and sometimes not due to this position:
I’m running this code:
public void MoveToPosition()
{
GraphNode vGraphNode = NextNode();
if (ToTurn)
{
this.enableRotation = false;
this.updateRotation = false;
Vector3 direction;
GraphNode vGrapghNode = NextNode();
if (vGrapghNode != null)
direction = (Vector3)vGrapghNode.position;
else if (!interpolator.valid)
direction = simulatedPosition;
else
direction = interpolator.tangent;
if (RotateBackward(direction))
ToTurn = false;
else
FinalizeMovement(tr.position, tr.rotation);
}
if (!ToTurn)
{
this.enableRotation = true;
this.updateRotation = true;
Vector3 nextPosition;
Quaternion nextRotation;
MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
FinalizeMovement(nextPosition, nextRotation);
}
}
public GraphNode NextNode()
{
if (this.path != null && this.path.path != null && this.path.path.Count > interpolator.segmentIndex + 1)
return this.path.path[interpolator.segmentIndex + 1];
else
return null;
}
public bool RotateBackward(Vector3 destination)
{
Vector3 direction = (destination - transform.position);
float vAngleResult = Vector2.Angle(new Vector2(transform.forward.x, transform.forward.z), new Vector2(direction.x, direction.z));
if (vAngleResult <= 11.5f)
return true;
Vector3 vDireccion = destination - transform.position;
Quaternion vRotation = Quaternion.LookRotation(vDireccion);
vRotation = Quaternion.RotateTowards(transform.rotation, vRotation, 20 * Time.deltaTime);
vRotation.x = transform.rotation.x;
vRotation.z = transform.rotation.z;
transform.rotation = vRotation;
direction = (destination - transform.position);
vAngleResult = Vector2.Angle(new Vector2(transform.forward.x, transform.forward.z), new Vector2(direction.x, direction.z));
bool vResult = vAngleResult <= 11.5f;
return vResult;
}
public void MovementUpdate(float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation)
{
if (updatePosition) simulatedPosition = tr.position;
if (updateRotation) simulatedRotation = tr.rotation;
Vector3 direction;
nextPosition = CalculateNextPosition(out direction, isStopped ? 0f : deltaTime);
if (enableRotation)
{
nextRotation = SimulateRotationTowards(direction, deltaTime);
nextRotation.x = this.gameObject.transform.rotation.x;
nextRotation.z = this.gameObject.transform.rotation.z;
}
else nextRotation = simulatedRotation;
}
public void FinalizeMovement(Vector3 nextPosition, Quaternion nextRotation)
{
previousPosition2 = previousPosition1;
previousPosition1 = simulatedPosition = nextPosition;
simulatedRotation = nextRotation;
if (updatePosition) tr.position = nextPosition;
if (updateRotation) tr.rotation = nextRotation;
}