PathFindingService (路径查找服务)

一种计算路径的服务。
Key Concepts (关键概念)Details (详情)
PathfindingService一种计算路径的服务。
Path表示已计算路径的对象。
Waypoints角色将要遵循的路径上的点。
Agent Parameters定义角色移动能力和限制的参数(例如,半径、高度、跳跃高度)。
Blocked Event路径被阻挡时触发的事件。
StatusChanged Event路径状态改变时触发的事件。

定义起点和终点

Image 1
Image 2
Roblox Studio
local PathfindingService = game:GetService("PathfindingService")
local character = workspace:WaitForChild("NPC")
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Anchored = false

-- Get the NPC's start position
local startPoint
if character.PrimaryPart then
	startPoint = character.PrimaryPart.Position
elseif character:FindFirstChild("HumanoidRootPart") then
	startPoint = character.HumanoidRootPart.Position
else
	error("No valid part found to determine the NPC's position")
end

local endPoint = workspace.End.Position

创建路径

-- Create a path object
local path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = true,
	AgentJumpHeight = 10,
	AgentMaxSlope = 45,
})

计算路径

-- Compute the path
path:ComputeAsync(startPoint, endPoint)

-- Check the path's status
if path.Status == Enum.PathStatus.Success then
	print("Path successfully computed.")
elseif path.Status == Enum.PathStatus.NoPath then
	print("No path could be found.")
elseif path.Status == Enum.PathStatus.Fail then
	print("Path computation failed.")
end

可视化路径

-- Visualize the path
local waypoints = path:GetWaypoints()

for _, waypoint in ipairs(waypoints) do
	local part = Instance.new("Part")
	part.Shape = Enum.PartType.Ball
	part.Material = Enum.Material.Neon
	part.Color = Color3.fromRGB(0, 255, 0)
	part.Size = Vector3.new(0.5, 0.5, 0.5)
	part.Position = waypoint.Position
	part.Anchored = true
	part.CanCollide = false
	part.Parent = workspace
end
Image 1
Roblox Studio

沿路径移动 NPC

-- Move the character along the path
for _, waypoint in ipairs(waypoints) do
	print(waypoint.Position)
	print(humanoid)
	humanoid:MoveTo(waypoint.Position)
	humanoid.MoveToFinished:Wait()
end

print("Character has followed the path.")
Image 1
Roblox Studio

示例:完整代码

local PathfindingService = game:GetService("PathfindingService")
local character = workspace:WaitForChild("NPC")
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Anchored = false

-- Get the NPC's start position
local startPoint
if character.PrimaryPart then
	startPoint = character.PrimaryPart.Position
elseif character:FindFirstChild("HumanoidRootPart") then
	startPoint = character.HumanoidRootPart.Position
else
	error("No valid part found to determine the NPC's position")
end

local endPoint = workspace.End.Position


-- Create a path object
local path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = true,
	AgentJumpHeight = 10,
	AgentMaxSlope = 45,
})

-- Compute the path
path:ComputeAsync(startPoint, endPoint)

-- Check the path's status
if path.Status == Enum.PathStatus.Success then
	print("Path successfully computed.")
elseif path.Status == Enum.PathStatus.NoPath then
	print("No path could be found.")
elseif path.Status == Enum.PathStatus.Fail then
	print("Path computation failed.")
end

-- Handle path being blocked during movement
path.Blocked:Connect(function(blockedWaypointIndex)
	print("Path is blocked at waypoint index:", blockedWaypointIndex)
end)

-- Visualize the path
local waypoints = path:GetWaypoints()

for _, waypoint in ipairs(waypoints) do
	local part = Instance.new("Part")
	part.Shape = Enum.PartType.Ball
	part.Material = Enum.Material.Neon
	part.Color = Color3.fromRGB(0, 255, 0)
	part.Size = Vector3.new(0.5, 0.5, 0.5)
	part.Position = waypoint.Position
	part.Anchored = true
	part.CanCollide = false
	part.Parent = workspace
end


-- Move the character along the path
for _, waypoint in ipairs(waypoints) do
	print(waypoint.Position)
	print(humanoid)
	humanoid:MoveTo(waypoint.Position)
	humanoid.MoveToFinished:Wait()
end

print("Character has followed the path.")

如果您觉得本教程对您有帮助,并且愿意支持我的工作,请考虑请我喝杯咖啡。

非常感谢您的支持!

请我喝咖啡