パス オブジェクトは、PathfindingService:CreatePath() によって作成されたパスの結果を保存します。
パスオブジェクトが作成されると、開始ポイントと終了ポイントを使って Path:ComputeAsync() を呼び出すことができます。これは、デフォルトまたはカスタムパラメータを CreatePath() に渡して、キャラクターが移動するための有効なパスを計算しようとします。If に成功してパスを見つけると、 オブジェクトには の値があります。そうでなければ、ステータスは Enum.PathStatus.NoPath になり、2つのポイント間に障害物があるか、ポイントが固体オブジェクトの内側にある場合に発生する可能性があります。
ComputeAsync() に加えて、Path オブジェクトには、キャラクターが順序に従ってパスの端から端へ移動する点を表すリストを返す GetWaypoints() メソッドがあります。
最後に、Pathは、 イベント にPath.Blockedできます。このイベントは、パスの存在期間中に、いつでもパスがブロックされる場合に発動します。これは、 通路沿いに移動するキャラクターの後ろに 発生する可能性があることに注意してください、ただし、前にだけではない。
概要
方法
パス内のポイントの配列を返します。
特定のウェイポイントから始まるパスがブロックされているかどうかをチェックします。
開始位置から終了位置まで Path を計算します。
イベント
計算されたパスがブロックされると発火します。
ブロックされていた計算されたパスがブロック解除されると、発火します。
プロパティ
方法
GetWaypoints
この関数は、PathWaypoints にあるすべての Path を含む配列を返し、Path:ComputeAsync() によって計算されます。
配列内の各ウェイポイントは、Vector3 位置と action を指定し、このパスウェイポイントに到達すると取得します。配列は、パスの開始から終了までのウェイポイントの順序で配置されます。
パスを計算できない場合、この関数は空の配列を返します。
戻り値
パスの開始からパスの終了わりまで順序付けられた PathWaypoints 配列。
コードサンプル
The example below demonstrates how to create a Path and it's PathWaypoints using the PathService.
It tries to Path:ComputeAsync() a path between two Vector3 positions, from pathStart to pathEnd. If a path is successfully created, indicated by it's Enum.PathStatus, the code block gets an array of its waypoints using Path:GetWaypoints(). Then, it loops through the array and prints each waypoint's position.
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local PATH_START = Vector3.new(0, 1, 0)
local PATH_END = Vector3.new(100, 1, 25)
path:ComputeAsync(PATH_START, PATH_END)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
print(waypoint.Position)
end
end
CheckOcclusionAsync
この機能は、 start で指定されたウェイポイントで始まるパスがブロックされているかどうかをチェックします。
ブロックされた場合は最初の障害地点を返し、そうでなければ -1 です。it returns an error if start が 0 未満または Path のウェイポイント数より大きい場合、エラーが返されます。
パラメータ
戻り値
ComputeAsync
この機能は、開始位置から終了位置まで Path を計算します。この機能は、パスが作成されると自動的に呼び出されず、パスを更新するたびに呼び出されなければなりません。
パスが計算されると、パスをたどるとキャラクターが道をたどることができる一連のウェイポイントが生じます。これらのポイントは Path:GetWaypoints() 関数で収集されます。
パラメータ
戻り値
コードサンプル
The code sample below explores how to move an NPC along a more complex path or around obstacles. This is known as pathfinding.
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)
-- When the path is blocked...
local function OnPathBlocked(blockedWaypointIdx)
-- Check if the obstacle is further down the path
if blockedWaypointIdx > currentWaypointIdx then
-- Recompute the path
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- Retrieve update waypoint list with path:GetWaypoints()
-- and Continue walking towards target
else
-- Error, path not found
end
end
end
path.Blocked:Connect(OnPathBlocked)
イベント
Blocked
計算されたパスがブロックされると発火します。道が、エージェントが逃げていく道に落ちるゴミの山のように、どこかでブロックされる可能性があることに注意してください 背後 エージェントが走っている。ブロックされたパスの処理 で、進路上のエージェントの前方ウェイポイント進行状況をチェックする方法の詳細は、を参照してください。
パラメータ
Unblocked
ブロックされていた計算されたパスがブロック解除されると、発火します。ブロックされたパスが、エージェントの後ろのどこかでブロックを解除する可能性があることに注意してください。これに反応することを効果的に不要にします。 後ろ のエージェント。ブロックされたパスの処理 で、進路上のエージェントの前方ウェイポイント進行状況をチェックする方法の詳細は、を参照してください。