I posted this question on the stack math site, but they are not too excited to see questions including programming. Anyway, now I am here :)
- I have an object, which is located at MyTransform.position (Vector)
- That object should follow the DesiredPosition (Vector), which is moving in varius directions with a changing velocity
- MaxDelayDistance (float) is the maximum distance my object is allowed to have to the DesiredPosition.
- DelayRecovery (float) are the seconds my object has in order to move to the DesiredPosition if the MaxDelayDistance is reached.
- Time.deltaTime (float) is a term we use to describe the time the last frame update took. It's value is usually about 0.025f.
private Vector3 GetLerpedPosition(Vector3 DesiredPosition) {
//DesiredPosition now contains the goal
Vector3 dirToDesiredPosition = (DesiredPosition - MyTransform.position).normalized; //Direction to desired position
Vector3 lerpStart = DesiredPosition + (-dirToDesiredPosition * MaxDelayDistance); //Start the lerp at my farthest allowed position
float lerpCurrentT = Vector3.Distance(MyTransform.position, DesiredPosition) / Vector3.Distance(lerpStart, DesiredPosition); //My current fragtion (t) of the lerp
//Now I lerp from lerpStart to DesiredPosition using a calculated fraction
Vector3 result = Vector3.Lerp(
lerpStart,
DesiredPosition,
lerpCurrentT + (DelayRecovery / MaxDelayDistance * Vector3.Distance(MyTransform.position, DesiredPosition) * Time.deltaTime)
);
return result;
}
The main problem is that my object is not following the DesiredPosition smoothly. It jumps from MaxDelayDistance to DesiredPosition and back. Somehow the fraction (t) in my Lerp function always results in about 1.005 or about 0.001. Can you spot any problems in my approach?