-1
\$\begingroup\$

I have tried to build a point and click mechanism in unity using

Vector2.Lerp(current_position,new_position,Time.deltatime)

But what I observed that the game object is moving at varying speeds depending on the distance between current position and new position.If the distance is too much, the acceleration is also increased and if the distances are low, the acceleration is low. But I want to acheive it in constant velocity. where do I alter ?

\$\endgroup\$
  • 1
    \$\begingroup\$ It's not a setting, it's math. if you want to move at a fixed velocity, you need to decide the velocity. Use the Vector for direction, and don't misuse the vector's length for speed, so: (new_position-current_position).normalized*velocity*time. \$\endgroup\$ – Peter Oct 17 '16 at 14:30
3
\$\begingroup\$

Lerp (Linear intERPolation) will move the vector by a fixed fraction of the distance towards the target. This implies two things:

  • The further the target is, the fastest the movement will be;

  • The overall movement between two points is not linear, but asymptotic: for example, with a fraction of 0.5 the moving point will cover half the distance in the first frame, then half the rest in the second frame, and so on.

Lerping is not the right tool for the job if you want a constant speed. Here's some code (C++-ish, but easily portable) that achieves constant speed:

// Moves from `pos` to `target` by `step` units, and returns that position.
// If `target` is within `step` units, returns `target`.
vec2 moveTowards(vec2 const pos, vec2 const target, float const step) {
    vec2 const delta = target - pos;      // Gap vector
    float const len2 = dot(delta, delta); // Squared length of the gap

    if(len2 < step * step)
        // We're close enough to close the gap in one step
        return target;

    // Unit vector that points from `pos` to `target`
    vec2 const direction = delta / sqrt(len2);

    // Perform the step
    return pos + step * direction;
}
\$\endgroup\$
  • \$\begingroup\$ In fact, Unity already has a built-in MoveTowards helper method on the Vector2 struct \$\endgroup\$ – DMGregory Oct 17 '16 at 15:14
  • \$\begingroup\$ @DMGregory hey, that does save a bit of work. But the documentation is really misleading then... \$\endgroup\$ – Quentin Oct 17 '16 at 15:19
  • \$\begingroup\$ Yeah, Unity documentation is not the greatest. I think they mean it's "like Lerp" in that it will return a vector somewhere on the line connecting the two inputs, but you're right that the method of calculating the point along that line is drastically different. \$\endgroup\$ – DMGregory Oct 17 '16 at 16:07
1
\$\begingroup\$

If you divide 1 by distance between the 2 points you get always the same velocity.

Vector2.Lerp(current_position,new_position,(1/Vector3.distance(current_position, new_position)) * Time.deltatime)

Btw: You can replace the 1 with a speed variable.

\$\endgroup\$

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.