PathfindingService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
PathfindingService ใช้เพื่อค้นหาเส้นทางที่เหมาะสมระหว่างสองจุด, ให้แน่ใจว่าตัวละครสามารถเคลื่อนที่ระหว่างจุดได้โดยไม่ต้องกัดผนังหรืออุปสรรคอื่น โดยปกติ, เส้นทางที่ส
ดู การหาเส้นทางตัวละคร สำหรับรายละเอียดการใช้งาน
สรุป
วิธีการ
ค้นหา Path ระหว่างสองจุดที่ให้
คุณสมบัติ
วิธีการ
CreatePath
สร้างวัตถุ Path ที่มีตัวแทนต่างๆ หลายอย่าง ของปุ่มและค่าในตาราง agentParameters ด้านล่างนี้คือ:
<tbody><tr><td><b>AgentRadius</b></td><td>ตัวเลข</td><td>2</td><td>กำหนดจำนวนพื้นที่แนวตั้งขั้นต่ำที่จำเป็นสำหรับพื้นที่ว่างเพื่อพิจารณาว่าพื้นที่นั้นสามารถเดินทางได้</td></tr><tr><td><b>ความสูงของเอเจนต์</b></td><td>ตัวเลข</td><td>5</td><td>กำหนดจำนวนพื้นที่แนวตั้งที่ต้องการสำหรับพื้นที่ว่างเพื่อพิจารณาว่าพื้นที่นั้นสามารถเดินทางได้</td></tr><tr><td><b>AgentCanJump ตัวแทน</b></td><td>บูลีน</td><td>จริง</td><td>กำหนดว่าคุณสามารถกระโดดในระหว่างการหาเส้นทางได้หรือไม่</td></tr><tr><td><b>AgentCanClimb เอเจนต์</b></td><td>บูลีน</td><td>ปลอม</td><td>กำหนดว่าคุณสามารถ <code>Class.TrussPart|TrussParts</code> ในระหว่างการนำทางได้หรือไม่</td></tr><tr><td><b>พื้นที่วายพอร์ต</b></td><td>จํานวน</td><td>4</td><td>กำหนดความห่างระหว่างจุดศูนย์กลางในเส้นทาง</td></tr><tr><td><b>ค่าใช้จ่าย</b></td><td>ตาราง</td><td></td><td>ตารางวัสดุหรือเรียกว่า <code>Class.Pathfindingmodified|PathfindingModifiers</code> และ "ค่าใช้จ่าย" สำหรับการเดินทาง มีประโยชน์สำหรับการทำให้เอเจนต์ชื่นชอบวัสดุ/ภูมิภาคบางอย่างมากกว่าคนอื่น ดูที่ <a href="../../../characters/pathfinding.md#pathfinding-modifiers">ที่นี่</a> สำ</td></tr></tbody>
กุญแจ | ชนิด | เริ่มต้น | คำอธิบาย |
---|
พารามิเตอร์
ตาราง Lua ที่ช่วยให้คุณปรับแต่งเส้นทางสำหรับขนาดของตัวแทน (มนุษย์ที่จะเคลื่อนทางตามเส้นทาง) ดูด้านบนสำหรับกุญแจ รูปแบบ และคำอธิบาย
ส่งค่ากลับ
ตัวอย่างโค้ด
Demonstrates creating a path using PathfindingService:CreatePath()
'PathOptions' represents a Model with three paths made of different materials. When creating the path we define a large 'Cost' for two of the three paths materials. This will ensure the path created steers towards the path with the material of the lowest 'Cost'.
Once the path is created, small parts are created at each waypoint to visualize the path.
local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- This model contains a start, end and three paths between the player can walk on: Snow, Metal and LeafyGrass
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- Create a path that avoids Snow and Metal materials
-- This will ensure the path created avoids the Snow and Metal paths and guides
-- the user towards the LeafyGrass path
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- Confirm the computation was successful
if success and path.Status == Enum.PathStatus.Success then
-- For each waypoint, create a part to visualize the path
for _, waypoint in path:GetWaypoints() do
local part = Instance.new("Part")
part.Position = waypoint.Position
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Color = Color3.new(1, 0, 1)
part.Anchored = true
part.CanCollide = false
part.Parent = Workspace
end
else
print(`Path unable to be computed, error: {errorMessage}`)
end
FindPathAsync
ฟังก์ชันนี้ใช้เพื่อค้นหา Path ระหว่างสองจุดที่ให้ไว้ เส้นทางนี้ใช้เครือข่ายการนำทางที่สร้างโดย PathfindingService และตรวจสอบว่าเส้นทางนี้สามารถติดตามโดยตัวละคร Roblox
ฟังก์ชันนี้จะกลับมาเป็น Path วัตถุที่มีค่าตัวของเส้นทาง หากไม่พบเส้นทางระหว่างสองจุดนี้ ฟังก์ชันนี้จะยังกลับมาเป็น Path ว
เพื่อรับเส้นทางของวัตถุ Path คุณสามารถใช้ฟังก์ชัน Path:GetWaypoints() ได้