tech/game

21/08/01 Roblox Studio Tutorial: Pathfinding

tech-lover 2021. 8. 1. 13:17

Roblox Studio Tutorial: Pathfinding (Creating a Path)

https://youtu.be/1j8itTPdoNE

Roblox Studio Tutorial: Pathfinding (Moving Objects on Path)

https://youtu.be/lC_iQkIvCK4

Roblox Studio Tutorial: Pathfinding (Moving NPC Humanoids)

https://youtu.be/pDlNwwKoAbw

Roblox Studio Tutorial: Pathfinding (Jump Over Obstacles)

https://youtu.be/tnKMbdh8wYs

 

-- Illustrate Path

```

local pathParts = game.Workspace.Path

local start = game.Workspace.Start

local finish = game.Workspace.Finish

local PathfindingService = game:GetService("PathfindingService")

local path = PathfindingService:CreatePath()

 

path:ComputeAsync(start.Position, finish.Position)

-- Get the path waypoints

local waypoints = path:GetWaypoints()

pathParts:ClearAllChildren()

-- Loop through waypoints

for _, waypoint in pairs(waypoints) do

    local part = Instance.new("Part")

    part.Shape = "Ball"

    part.Material = "Neon"

    part.Size = Vector3.new(0.60.60.6)

    part.Position = waypoint.Position

    part.Anchored = true

    part.CanCollide = false

    part.Parent = pathParts

end

```

 

- Move Part on Path

```

local pathParts = game.Workspace.Path

local start = game.Workspace.Start

local finish = game.Workspace.Finish

local PathfindingService = game:GetService("PathfindingService")

 

local path = PathfindingService:CreatePath()

path:ComputeAsync(start.Position, finish.Position)

-- Get the path waypoints

local waypoints = path:GetWaypoints()

pathParts:ClearAllChildren()

 

-- Loop through waypoints

for _, waypoint in pairs(waypoints) do

    local part = Instance.new("Part")

    part.Shape = "Ball"

    part.Material = "Neon"

    part.Size = Vector3.new(0.60.60.6)

    part.Position = waypoint.Position

    part.Anchored = true

    part.CanCollide = false

    part.Parent = pathParts

    start.Position = waypoint.Position

    wait(0.1)

end

```

 

-- Redraw Path

```

local pathParts = game.Workspace.Path

local start = game.Workspace.Start

local finish = game.Workspace.Finish

local PathfindingService = game:GetService("PathfindingService")

 

while true do

    local path = PathfindingService:CreatePath()

    path:ComputeAsync(start.Position, finish.Position)

    -- Get the path waypoints

    local waypoints = path:GetWaypoints()

    pathParts:ClearAllChildren()

 

    -- Loop through waypoints

    for _, waypoint in pairs(waypoints) do

        local part = Instance.new("Part")

        part.Shape = "Ball"

        part.Material = "Neon"

        part.Size = Vector3.new(0.60.60.6)

        part.Position = waypoint.Position

        part.Anchored = true

        part.CanCollide = false

        part.Parent = pathParts

    end

    wait(0.1)

end

```

 

-- Object Tracking

```

local start = game.Workspace.Start

local finish = game.Workspace.Finish

local PathfindingService = game:GetService("PathfindingService")

 

while true do

    local path = PathfindingService:CreatePath()

    path:ComputeAsync(start.Position, finish.Position)

    -- Get the path waypoints

    local waypoints = path:GetWaypoints()

    -- Loop through waypoints

 

    for i = 15 do

        if i <= #waypoints then

            local part = Instance.new("Part")

            part.Shape = "Ball"

            part.Material = "Neon"

            part.Size = Vector3.new(0.60.60.6)

            part.Position = waypoints[i].Position

            part.Anchored = true

            part.CanCollide = false

            part.Parent = pathParts

            start.Position = waypoints[i].Position

            wait(.1)

        end

    end

end

```

 

-- Moving Humanoid Models

```

local pathParts = game.Workspace.Path

local start = game.Workspace.Start

local finish = game.Workspace.Finish

local dummy = game.Workspace.Dummy

local humanoid = dummy.Humanoid

local PathfindingService = game:GetService("PathfindingService")

local path = PathfindingService:CreatePath()

 

path:ComputeAsync(dummy.Torso.Position, finish.Position)

-- Get the path waypoints

local waypoints = path:GetWaypoints()

pathParts:ClearAllChildren()

 

-- Loop through waypoints

for _, waypoint in pairs(waypoints) do

    local part = Instance.new("Part")

    part.Shape = "Ball"

    part.Material = "Neon"

    part.Size = Vector3.new(0.60.60.6)

    part.Position = waypoint.Position

    part.Anchored = true

    part.CanCollide = false

    part.Parent = pathParts

end

 

for _, waypoint in pairs(waypoints) do

    humanoid:MoveTo(waypoint.Position)

    humanoid.MoveToFinished:Wait()

end

```

 

-- Humanoid Model Object Tracking

```

local pathParts = game.Workspace.Path

local finish = game.Workspace.Finish

local dummy = game.Workspace.Dummy

local humaoid = dummy.Humanoid

local PathfindingService = game:GetService("PathfindingService")

 

while true do

    local path = PathfindingService:CreatePath()

    path:ComputeAsync(dummy.Torso.Position, finish.Position)

    local waypoints = path:GetWaypoints()

 

    for i = 15 do

        if i <= #waypoints then

            if waypoints[i].Action == Enum.PathWaypointAction.Jump then

                humaoid:ChangeState(Enum.HumanoidStateType.Jumping)

            end

            humaoid:MoveTo(waypoints[i].Position)

            humaoid.MoveToFinished:Wait()

        end

    end

end

```