PathfindingService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
PathfindingService は、2つのポイント間のロジカルなパスを見つけるために使用されます。これにより、キャラクターが壁や他の障害物に遭遇せずにポイント間を移動できます。デフォルトでは、最も短いパスが計算されますが、壁や他の障害物を回避するためのパスファインダーを実装して、さまざまな素材、周囲
詳細は、「キャラクターパスファインディング」を参照してください。
概要
方法
指定された 2つのポイントの間で Path を見つけます。
プロパティ
方法
CreatePath
さまざまなエージェントパスラメータに基づく Class.Path オブジェクトを作成します。 agentParameters テーブルの有効なキーと値は次のとおりです:
<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>エージェントCanJump</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.PathfindingModifier|PathfindingModifiers</code> とその "cost" で材料/領域を移動する。 ユーザーエージェントが特定の材料/領域を他のより好むようにするのに便利。詳細は、<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 を 2つの指定されたポイント間で見つけます。このパスは、PathfindingService によって作成されたナビゲーショングリッドを使用し、通常サイズの Roblox キャラクターがパスをフォローできるようにします。
この関数は、Path オブジェクトを返します。このオブジェクトには、パスの座標が含まれています。2つのポイント間でパスが見つからない場合、この関数はまだ Path オブジェクトを返しますが、そのオブジェクトの Path.Status は 2>En
Class.Path オブジェクトのウェイポイントを取得するには、Path:GetWaypoints() 関数を使用できます。