Not overshooting target

This is the solution I came to, is there any reason not to use this ?

    float moveSpeed = 4.0f;
    Vector3 moveDir = (targetPosition - transform.position).normalized;
    float distToTarget = Vector3.Distance(targetPosition, transform.position);

    Vector3 predictedPosition = transform.position + moveSpeed * Time.deltaTime * moveDir; 
    float predictedDistToTarget = Vector3.Distance(predictedPosition, transform.position);

    if (distToTarget <= predictedDistToTarget)
    {
        transform.position = targetPosition;
    }
    else
    {
        transform.position = predictedPosition;
    }

No, this looks like it should work. There are often many paths to the same goal.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms