PathfindingService

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ
ไม่ซ้ำ

บริการค้นหาเส้นทาง ใช้เพื่อค้นหาเส้นทางโลจิสติกระหว่างจุดสองจุด เพื่อให้แน่ใจว่าตัวอักษรสามารถเคลื่อนที่ระหว่างจุดได้โดยไม่ชนกำแพงหรืออุปสรรคอื่นโดยค่าเริ่มต้นจะคำนวณเส้นทางที่สั้นที่สุด แต่คุณสามารถใช้ตัวแก้ไขเส้นทางเพื่อคำนวณเส้นทางที่ชาญฉลาดขึ้นผ่านวัสดุต่างๆ รอบพื้นที่ที่กำหนดหรือผ่านอุปสรรค

ดู การค้นหาเส้นทางตัวอักษร สำหรับรายละเอียดการใช้งาน

คุณสมบัติ

วิธีการ

CreatePath

สร้างวัตถุ Path ตามพารามิเตอร์ตัวแทนหลายรายการ คีย์และค่าที่ถูกต้องในตาราง agentParameters มีดังนี้:


<th>ประเภท</th>
<th>ค่าเริ่มต้น</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>เอเจนต์รัศมี</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>ตัวแทนสามารถกระโดด</b></td>
<td>เป็นไปได้</td>
<td>จริง</td>
<td>กำหนดว่าการกระโดดระหว่างการค้นหาเส้นทางจะได้รับอนุญาตหรือไม่</td>
</tr>
<tr>
<td><b>ตัวแทนสามารถปีนได้</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>
กุญแจ

พารามิเตอร์

agentParameters: Dictionary

ตาราง Luau ซึ่งช่วยให้คุณปรับแต่งเส้นทางสำหรับขนาดของตัวแทน (โฮมูนิดที่จะเคลื่อนที่ไปตามเส้นทาง)ดูด้านบนสำหรับกุญแจที่ถูกต้องประเภทและคำอธิบาย

ค่าเริ่มต้น: "nil"

ส่งค่ากลับ

วัตถุ Path

ตัวอย่างโค้ด

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.

Creating a Path with Pathfinding Service

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:GetWaypoints()

พารามิเตอร์

start: Vector3

พิกัดเริ่มต้นเส้นทาง

ค่าเริ่มต้น: ""
finish: Vector3

พิกัดจุดสิ้นทาง

ค่าเริ่มต้น: ""

ส่งค่ากลับ

วัตถุ Path

อีเวนต์