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
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.
- (Assuming a radius of 100)
if (finish.Position - start.Position).magnitude < 100 then
...
- (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
end
if length < 141 then
...
Asides:
- It probably isn't a good idea to compute a path so frequently
1 comment
Wow amazing explanation thank you :).