0
\$\begingroup\$

In Unity's documentation for Vector2.MoveTowards, they assign a two variables, "speed" and "step".

"step" is simply speed * Time.deltaTime.

Why is this step variable assigned? What function does this serve that means the speed variable isn't used in place of step? What is this variable controlling when used in these parameters?

\$\endgroup\$
1
  • \$\begingroup\$ You could put transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime); all in one line, but naming that last argument step makes the line a bit shorter, and clarifies intent. (Now I don't have to ask "why is speed being multiplied by delta time?" - the variable name clarifies "oh, that's the distance of one step in the movement") \$\endgroup\$
    – DMGregory
    Jan 10, 2022 at 23:22

1 Answer 1

1
\$\begingroup\$

This variable is the maximum distance the object is allowed to move on this method-call.

You usually call MoveTowards in Update in order to move an object one step closer to its destination. But the frequency of Update calls depends on the current framerate. So on lower framerates you need to move the object further per update than on higher framerates. Which is what Time.deltaTime is for. It represents the time since the last update.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .