Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How to get from point a to point b?

Asked by 9 years ago

Both are a position, the catch is it has to use a for loop and doesn't just go to point b, it slowly moves there. I know this, just I have overthought it and I can't think straight.

0
If you're going to downvote for no reason, at least explain why EverythingScripted 0 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

It's useful to think about problems involving vectors relative to some nice point.

For this problem, think relative to start.

Relative to start, stop is stop - start.

Thus as we travel we have to go from Vector3.new() to stop - start relative to start.

The simplest thing that goes from nothing to a value is a line: this is a simple case of linear interpolation. You go 5%, 10%, 15%, 20%, etc, just by multiplying your progress:

for t = 0, 1, 0.03 do
    local movement = (stop - start) * t
    local position = start + movement
    wait()
end

With this method, it will always take exactly 1 second to get from start to stop, no matter how far or close they are. Depending on your application, that may be what you want, but for actual motion, it probably isn't.

A simple solution is to adjust the rate inversely to the distance:

local speed = 10 -- studs / second
local distance = (stop - start).magnitude
local rate = 0.03 / distance * speed
for t = 0, 1, rate do
    -- same as before
0
Works but makes my player glitch out in circles when moving towards point b but it does go to point b EverythingScripted 0 — 9y
Ad

Answer this question