I am moving my camera from origin to destination, it all works well but the problem is that there is no smooth ease in/out from origin to destination. How do I achieve a smooth ease in when camera is travelling and smooth ease out when the camera reaches the destination? It is necessary to have totalMovementTime as I want my camera to reach the destination in given seconds.
public IEnumerator MoveToDestination () {
float totalMovementTime = 2f;
float currentMovementTime = 0f;
while (Vector3.Distance (transform.localPosition, destinationCamPos) > 0) {
currentMovementTime += Time.deltaTime;
transform.localPosition = Vector3.Lerp (transform.localPosition, destinationCamPos, currentMovementTime / totalMovementTime);
yield return null;
}
}
You can use the AnimationCurve for "easy" easing :) Create a serialized field in your script of type AnimationCurve. In inspector play with it making the desired easing curve (there are also ready to use presets). Then for the step (progress) of your lerp you need to "evaluate" using your curve AnimationCurve.Evaluate Make sure your are using the normalized value for the Lerp progress.
[SerializedField] AnimationCurve curve;
...
var normalizedProgress = currentMovementTime / totalMovementTime ; // 0-1
var easing = curve.Evaluate(normalizedProgress);
transform.localPosition = Vector3.Lerp (transform.localPosition, destinationCamPos, easing);