# Moving object to point a to point b smoothly

I am working on a problem where distance between the point A and B or given and object of interest should move smoothly from A to B.

I can control only control the speed of the object using this object should start slowly from point ease in and ease out at point B and need to exactly land on point B

I tried implementing ease in speed using distance covered however, at the beginning distance covered will be zero there will be starting problem. I solved starting problem using time elapsed as reference however this will not work precisely stoping object at point B, so I used distance remaining as a key to ease out.

This works for larger distance for smaller distance yet to tweak the code . Overall implementation messy not satisfied. Is there any generic solution to solve this problem? Note speed just a parameter controls overall movement of object, I can't say there is a exact relationship speed and distance covered.

(Unity developers: Note I am trying to control the movement of the character using three animation idle:speed=0, slowwalk:average speed=1, fastwalk: average speed=2)

• I've interpolated using a sigmoid curve in the past. Would that work for you?
– RobM
Commented May 12, 2016 at 8:32

If I understand your question correctly, you want to smoothly move an object from point A to point B.

I would use a coroutine:

private IEnumerator Move_Routine(Transform transform, Vector3 to, float speed)
{
Vector3 from = transform.position;
float distance = Vector3.Distance(from, to);
float rate = speed / distance;

for (float t = 0; t < 1; t += rate * Time.deltaTime)
{
transform.position = Vector3.Lerp(from, to, Mathf.SmoothStep(0f, 1f, t));
yield return null;
}

// Ensure we end exactly at the destination,
// since the loop can end a fraction away.
transform.position = to;
}


And I would start it by calling:

StartCoroutine(Move_Routine(this.transform, Vector3.one, 1));

• He wants it to ease in and our rather than linear interpolate I think
– RobM
Commented May 13, 2016 at 15:24
• @RobM my code is using a smooth step in there, and if that's not smooth enough you can always do this Mathf.SmoothStep(0f, 1f, Mathf.SmoothStep(0f, 1f, Mathf.SmoothStep(0f, 1f, t)));
– Iggy
Commented May 13, 2016 at 15:26
• Aha! SmoothStep. Wasn't aware that it performs a sigmoid type of interpolation.
– RobM
Commented May 14, 2016 at 18:41