'Unity2d: SmoothDamp not moving to Lerp-ed calculated position

travelTime = 0.0f;
            while (Vector3.Distance(Vector2.Lerp(startPos, endPos, travelTime), transform.position) >= maxDistance) {
                locationToGo = Vector2.Lerp(startPos, Input.mousePosition, travelTime);
                travelTime += 0.1f;
            }
            // SHOW
            // Smoothly move the camera towards that target position
            transform.position = Vector3.SmoothDamp(transform.position, locationToGo, ref velocity, smoothTime);

So, this code is meant to use Lerp to return a Vector2 a certain amount of 'steps' towards the mouse pointer. And then give that value to the SmoothDamp to move the square to the pos. But the Lerp is not calculating correctly. Does anyone know what is wrong, or any better working alternatives?



Solution 1:[1]

Since I don't really know what exactly you are trying to achieve i'm just gonna guess you want to move a GameObject to the MousePosition every Frame. You could do it like this (it is out of my head, not compileproof):

GameObject ourGameobject;
Vector3 ourTarget;
void Update() 
{
    ourGameobject.transform.position = Vector3.lerp(ourGameobject.transform.position, ourTarget, Time.DeltaTime);
}
    

For faster or lower speed just multiply Time.DeltaTime with sth.

Also Note that Input.MousePosition returns a coordinate on the screen.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cpaech