エンジンクラス
PathfindingService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
プロパティ
方法
ComputeRawPathAsync(start: Vector3,finish: Vector3,maxDistance: number):Path |
ComputeSmoothPathAsync(start: Vector3,finish: Vector3,maxDistance: number):Path |
CreatePath(agentParameters: Dictionary):Path |
FindPathAsync(start: Vector3,finish: Vector3):Path |
コードサンプル
経路探索サービスの使用
local PathfindingService = game:GetService("PathfindingService")
local agentParams = {
AgentRadius = 2.0,
AgentHeight = 5.0,
AgentCanJump = false,
}
local currentWaypointIdx = 1
local path = PathfindingService:CreatePath(agentParams)
local humanoidRootPart = script.Parent:FindFirstChild("HumanoidRootPart")
local targetPosition = Vector3.new(50, 0, 50)
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
-- 経路がブロックされている場合...
local function OnPathBlocked(blockedWaypointIdx)
-- 障害物が経路のさらに先にあるか確認
if blockedWaypointIdx > currentWaypointIdx then
-- 経路を再計算
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- path:GetWaypoints()で更新された経路ポイントリストを取得
-- そしてターゲットに向かって歩き続ける
else
-- エラー、経路が見つかりません
end
end
end
path.Blocked:Connect(OnPathBlocked)APIリファレンス
プロパティ
EmptyCutoff
方法
ComputeRawPathAsync
ComputeSmoothPathAsync
CreatePath
パラメータ
| 既定値: "nil" |
戻り値
コードサンプル
Pathfindingサービスを使用してパスを作成する
local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- このモデルは、プレイヤーが歩けるスタート、エンド、3つのパスを含んでいます:Snow、Metal、LeafyGrass
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- SnowおよびMetal材質を避けるパスを作成します
-- これにより、作成されたパスはSnowおよびMetalパスを避けて、
-- ユーザーをLeafyGrassパスへ誘導します
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- パスを計算します
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- 計算が成功したことを確認します
if success and path.Status == Enum.PathStatus.Success then
-- 各ウェイポイントのために、パスを可視化するパーツを作成します
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(`パスを計算できませんでした。エラー: {errorMessage}`)
endFindPathAsync