Answered by
6 years ago Edited 6 years ago
Here's a quick mock-up I made. It's not perfect but it should set you in the right direction. Server-side script.
01 | local player = game.Workspace:WaitForChild( "Cousin_Potato" ) |
02 | local head = player.Head |
04 | local breadcrumbDistance = 5 |
05 | local distanceTraveled = 0 |
06 | local previousTime = tick() |
08 | local function placeBreadcrumb(position) |
09 | local breadcrumb = Instance.new( "Part" ) |
10 | breadcrumb.Position = position |
11 | breadcrumb.Anchored = true |
12 | breadcrumb.CanCollide = false |
13 | breadcrumb.Parent = game.Workspace |
17 | local ET = tick() - previousTime |
20 | distanceTraveled = distanceTraveled + ET * (head.Velocity.Magnitude) |
21 | print (distanceTraveled) |
22 | if distanceTraveled > breadcrumbDistance then |
23 | placeBreadcrumb(head.Position) |
First thing, the elapsed time is updated to determine how long it has been since the last loop.
The velocity magnitude is simply the total velocity in all directions combined. If the player is moving unimpeded, they will all add up to Humanoid.Walkspeed. We can use this to calculate the distance traveled by multiplying by the E.T. since the last time around the loop.
Finally, if the distance is greater than the breadcrumb distance, place a breadcrumb and reset the distance.
*This is an accumulative function, meaning the distance adds regardless of direction. That means if your character runs in a small circle, breadcrumbs will still be added once the distance is exceeded, even if the distance to the last breadcrumb is less than breadcrumbDistance. This isn't a tough problem so I'll let you figure that one out.
Within the if statement, rather than creating news parts, you can push the position onto an array and use that for your AI to follow.
Just for clarification, the MoveTo method does not use pathfinding, so as long as the AI can reach the first/nearest breadcrumb directly, you can forgo pathfinding.
The pathfinding service path includes information about whether the AI should walk or jump. This simple script does not so you'll have to implement your own for jumping over obstacles.
Obviously, the first two lines are for the mock-up so don't actually use that to find your player.