How to move object in steps

I am new to unity and I am working on my first project. I am creating a game where the movement is similar to Snake and Ladders. If the object is on number 9 and the dice returns 5, the item should move to 10, pause for a very small duration, move to 11, pause, move to 12, pause and so on and finally stop at 14. I am currently using

transform.position = Vector3.MoveTowards(transform.position,newPosition,speed);

The above code directly takes the object to the final location.
I tried a for loop that moves the object one step each time but I found that it directly takes the object to the final position. What is the correct way to move an object to another position in steps, though a set of way-points?

The Snake and Ladder game in this video has the kind of movement I am looking for:

You may use Vector3.Lerp (Unity - Scripting API: Vector3.Lerp)

It take the source position, the destination position and a float between 0 and 1.
0 will return the source position and 1 the destination, any value between will be interpolated.

You can do something like this:

public float t = 0;
public Vector3 srcPosition;
public Vector3 dstPosition;
void Update()
{
     transform.position = Vector3.Lerp(srcPosition, dstPosition, t);
     t += Time.deltaTime * 0.5f; // will do the lerp over two seconds
}

The make it more sexy, you can use an AnimationCurve to make elastic effects, bounces effects… (Unity - Scripting API: AnimationCurve)
It can be used like this:

public AnimationCurve curve;
public float t = 0;
public Vector3 srcPosition;
public Vector3 dstPosition;
void Update()
{
     transform.position = Vector3.Lerp(srcPosition, dstPosition, curve.Evaluate(t));
     t += Time.deltaTime * 0.5f; // will do the lerp over two seconds
}

Here are the most used curves: Tween.js / graphs

Vector3.Lerp can also be used like this:

public Vector3 dstPosition;
void Update()
{
     transform.position = Vector3.Lerp(transform.position, dstPosition, Time.deltaTime * 0.5f);
}

But the movement will be logarithmic like the Quadratic.Out curve

How do I use this to move from Point A to Point B, wait for a second and then move to Point C?

You have two easy possibilities.

You use the Invoke function to call a void after some time.

For example:

private void Start()
{
Invoke("NextPoint", 1)
}
private void NextPoint()
{
print("Is called after 1 seconds)
}

Either you use a coroutine:

private void Start()
{
StartCoroutine(TweenPosition(new Vector3(0, 0, 0), new Vector3(0, 0, 1)));
}
    private IEnumerator TweenPosition(Vector3 src, Vector3 dst)
    {
        float t = 0;
        while(t <= 1)
        {
            transform.position = Vector3.Lerp(src, dst, t);
            t += Time.deltaTime;
            yield return 0; // wait one frame to refresh display
        }

        yield return new WaitForSeconds(1); // wait one second

        // call again TweenPosition as a coroutine
    }
}

What I would have done myself for a better optimization is a script used just for that with the interpolation done in an Update() function that have a public Play function.

The script would have automatically disable himself when the tween end and the Play function reset the “t” var and enable the script.
You can call a function from a script disabled that enable himself, that’s not a problem.

The 1 second timer between movements would have been done somewhere else.

1 Like