1
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ I've interpolated using a sigmoid curve in the past. Would that work for you? \$\endgroup\$ – RobM May 12 '16 at 8:32
1
\$\begingroup\$

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 from, Vector3 to)
{
    float t = 0f;
    while(t < 1f)
    {
        t += Time.deltaTime;
        transform.position = Vector3.Lerp(from, to, Mathf.SmoothStep(0f, 1f, t));
        yield return null;
    }
}

And I would start it by calling:

StartCoroutine(Move_Routine(this.transform, Vector3.zero, Vector3.one));
\$\endgroup\$
  • \$\begingroup\$ He wants it to ease in and our rather than linear interpolate I think \$\endgroup\$ – RobM May 13 '16 at 15:24
  • \$\begingroup\$ @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))); \$\endgroup\$ – Iggy May 13 '16 at 15:26
  • \$\begingroup\$ Aha! SmoothStep. Wasn't aware that it performs a sigmoid type of interpolation. \$\endgroup\$ – RobM May 14 '16 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.