- Home /
Lerp / SmoothDamp complete before t=1
I have recently noticed what I would consider to be some strange behavior in one of my games. I am noticing that when I move an object/change a value using Mathf.SmoothDamp or Vector2/3.Lerp the value appears to reach it's "to" value before the t variable ever hits 1!
Here's an example! I'm using this code to teleport objects around the screen. I'm normally moving the object around 1-2 units on one axes. I am seeing the object movement complete .61f that seems odd right?
public IEnumerator teleport (Vector2 direction) {
Debug.Log("start");
direction = new Vector2(transform.position.x + direction.x, transform.position.y + direction.y);
direction *= teleportationLength;
Vector3 destinationV3 = new Vector3(direction.x, direction.y, transform.position.z);
float teleportationProgress = 0f;
sprite.enabled = false;
StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize + teleportationCameraOffset));
while (teleportationProgress < 1) {
if (transform.position == destinationV3)
Debug.Log("DONE: " + teleportationProgress);
transform.position = Vector2.Lerp(transform.position, direction, teleportationProgress);
teleportationProgress += Time.deltaTime * teleportationSpeed;
yield return null;
}
sprite.enabled = true;
StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize - teleportationCameraOffset));
Debug.Log("stop");
}
Answer by flamy · Apr 22, 2014 at 06:04 AM
lerp should be from startPosition to endPosition, zero on time is at startPosition and 1 will be at endPosition.... having a variable in one of this will result in odd behaviour. In your case you are using transform.position which is variying every frame, meaning your start position changes every frame or after every time the position changes. and for telePortation you have complicated your logic more than necessary...
I suppose your direction is +x or -x so have Vector3.right or left for direction and multiply it with teleporation distance, and add it with current transform position. Now this is the destination and current position is your start position. and now it would me much more simple, check the code below....
public IEnumerator teleport (Vector2 direction) {
direction *= teleportationLength;
Vector3 destinationV3 = transform.position+direction;
Vector3 startPosition = transform.position;
float teleportationProgress = 0f;
sprite.enabled = false;
StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize + teleportationCameraOffset));
while (teleportationProgress < 1) {
if (transform.position == destinationV3)
Debug.Log("DONE: " + teleportationProgress);
transform.position = Vector2.Lerp(startPosition , destinationV3 , teleportationProgress);
teleportationProgress += Time.deltaTime * teleportationSpeed; // you can have it as Time.deltaTime/TimeTakenForTeleportation.... this works fine too... but here idk wat yor teleportation speed value is... hope it works fine.
yield return null;
}
transform.position = destinationV3 ;
sprite.enabled = true;
StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize - teleportationCameraOffset));
Debug.Log("stop");
}
PS: this is untested code!!
@!$%!@#! Thank you so much man! What a bone head mistake I was making!