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

I need help with pathfinding?

I have the pathfinding lines showing but i can't figure out how to make the brick follow me after a certain distance i know i can use magnitude but even if i do use magnitude i still don't know how to make the brick move towards my body can anyone help?

Points = Instance.new("Model",Workspace)
Points.Name = "Points"
start = Instance.new("Part",Workspace)

while true do
	wait()
finish = game.Workspace.Player1.Torso
game.Workspace.Points:ClearAllChildren()

path = game:GetService("PathfindingService"):ComputeRawPathAsync(start.Position,finish.Position,500)
points = path:GetPointCoordinates()

for i = 1,#points do
	lines = Instance.new("Part",game.Workspace.Points)
	lines.BrickColor = BrickColor.new(21)
	lines.Size = Vector3.new(1,1,1)
	lines.Position = points[i]
end
end

1 answer

✓ Accepted answer

Your question isn't very clear, so I am just going to give the best explanation I can. I'm assuming you want start to move along the path towards its target.

(From Q8503)

When we move from one point to another, we move a certain distance in a certain direction. That quantity is a vector (a distance in a particular direction). The vector meaning the path from one point to another is called the displacement from the start point to the end point.

The displacement from point A to point B is B - A.

This makes sense since if we start at A, we move the displacement, and we should get at B. That is, A + D = B, so D = B - A.

So to find the direction and distance from from to to, we do displacement = to - from.


However, let's say we want to only move 10 studs in that direction. If to and from were very far apart, then displacement will be much larger than 10 in magnitude. So we use use its unit vector, or the vector in the same direction but with a magnitude of 1.

direction = displacement.unit

We can now multiply direction by the distance we want to move (e.g., 10, or 0.1, or 3, &c)


For this snippet, our target (to) would be points[1] or points[2]. I'm going to use points[2] since it might make the path slightly smoother in appearance, although if there are very tight turns this might make walking fail sometimes.

The from is where we are - start.Position

target = points[2]
from = start.Position
direction = (target - from).unit

Now, to move start in that direction, we can CFrame it:

start.CFrame = start.CFrame + direction / 30 * 10
-- Approximately 10 studs / second, or ~2/3 of walking speed

If wanted this following to only occur when the player is close, we have two options. 1) Take the direct distance to the player (ignoring obstacles), which is much simpler, or 2) compute the length of the path.

  1. (Assuming a radius of 100)
if (finish.Position - start.Position).magnitude < 100 then
	...
  1. (Assuming a radius of 141, slightly larger since this will have to walk around walls)
local length = 0
for i = 1, #points - 1 do
	length = length + (points[i] - points[i + 1]).magnitude
	-- Add up the distance between adjacent points
end
if length < 141 then
	...



Asides:

  • It probably isn't a good idea to compute a path so frequently

1 comment

0

Wow amazing explanation thank you :).

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