Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to get from point a to point b?

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.

1 comment

0

If you're going to downvote for no reason, at least explain why

1 answer

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

1 comment

0

Works but makes my player glitch out in circles when moving towards point b but it does go to point b

This discussion is preserved as it was. New answers, comments, and votes are closed.