Slerp not working?

Hi Guys,

I was using Vector3.Lerp to create a load of actions in a coroutine. I now need to make this have an ‘ease in’ ‘ease out’ effect. I found that Vector3.Slerp was likely to do that, however I’m seeing no change in effects. Can anyone explain?

Here’s my code:

for(int i=0; i < endPos.Length; i++)
        {
            incrPosition = obj.transform.position;

            for (float t = 0; t < _duration; t += Time.deltaTime)
            {
                float dt = t / _duration;
                obj.transform.position = Vector3.Slerp(incrPosition, endPos[i].transform.position, dt);

                yield return new WaitForFixedUpdate();
            }
            incrPosition = endPos[i].transform.position;

            // Scale
            Vector3 origScale = obj.transform.localScale;
            Vector3 smallerScale = (origScale * 0.5f);
            for (float t = 0; t < 0.25f; t += Time.deltaTime)
            {
                float dt = t / _duration;
                obj.transform.localScale = Vector3.Slerp(obj.transform.localScale, smallerScale, dt);

                yield return new WaitForFixedUpdate();
            }
            for (float t = 0; t < 0.25f; t += Time.deltaTime)
            {
                float dt = t / _duration;
                obj.transform.localScale = Vector3.Slerp(obj.transform.localScale, origScale, dt);

                yield return new WaitForFixedUpdate();
            }

            yield return new WaitForSeconds(_moveInterlude);
        }

After googling it says that Slerp is something about directions and circles as opposed to two points in space. I’m not quite sure HOW to use that to make a ‘ease in’ ‘ease out’ effect. Can someone clue me in?

Thanks!

Slerp allows your “points” to be “directions” (magnitude having an effect), which doesn’t actually help here unless you’re planning on directly manipulating the magnitude over time, which is possible but more complicated. Instead, do this:

for (float t = 0; t < _duration; t += Time.fixedDeltaTime)
{
    float dt = t / _duration;
    obj.transform.position = Vector3.Lerp(incrPosition, endPos[i].transform.position, Mathf.SmoothStep(0f, 1f, dt));

    yield return new WaitForFixedUpdate();
}
1 Like

Works perfect, thanks :). So what’s the deal with Slerp then?

It’s so that you can curve the movement itself, “spherical lerp”, not the timing for it. The example given on the documentation is a “rising and setting sun”, so that should give you an idea :slight_smile:

1 Like