Difference between revisions of "API:Class/PathfindingService"

 
Line 13: Line 13:
 
In this example, PathfindingService is used to find a path between two parts. The path is then drawn using LineHandleAdornments. The path will be colored in yellow for the segments where a character would need to jump.
 
In this example, PathfindingService is used to find a path between two parts. The path is then drawn using LineHandleAdornments. The path will be colored in yellow for the segments where a character would need to jump.
  
To see this example in action, please visit [https://www.roblox.com/games/1086122752/Pathfinding-Service-Example this] place. The place is not copy-locked so it can be opened in Studio by anyone.
+
{{Sample|https://www.roblox.com/games/1086122752/Pathfinding-Service-Example}}
  
 
{{Code|=
 
{{Code|=

Latest revision as of 20:09, 12 October 2017

Filtered-inheritance mode: While this class technically inherits all Instance class members, some (if not all) have been hidden because they serve no purpose for this object.
Service: This class is a service. It is a top-level singleton which can be obtained with the GetService method.
Uncreatable: This object cannot be created with Instance.new.

Forcefield icon.pngPathfindingService : Object Icon.pngInstance

PathfindingService is used to find a path between two points. The paths that the service finds are meant for regular sized Roblox characters. The paths make sure the characters can move between the points without running into walls or other obstacles. Paths will not only show where a character needs to walk to reach their goal, but will also show if the character will need to jump to get over gaps. Paths can be used both for characters controlled by players and for non-player characters.

PathfindingService generates a navigation mesh over all of the parts in the place while a game is running. This mesh indicates where a regular sized character can walk. Any path that is created with the service will stay within the mesh. If the geometry of the place changes, for example if a part moves or is created, then the navigation mesh will be recaulculated.

To see the navigation mesh for a place, open the place in Studio, navigate to File > Settings, and toggle on the setting Show Navigation Mesh. The mesh will then show up in the 3D view. The purple areas in the place show where a character can walk. The areas that are not colored in are considered blocked for characters. Studio also displays arrows on top of the mesh. These show where a character would have to jump in order to get from one part of the mesh to another.

NavigationMesh.png

PathfindingService has one function called FindPathAsync which is used to find paths. This function takes two parameters: the starting point of the path and the end point. This function then returns a Path object. To learn more about how the Path object works, please read this page.

Properties

PropertiesmemberhiddenProperties [toggle]

floatEmptyCutoff [deprecated]

Deprecated. Do not use.

|RMD member="API:Class/PathfindingService/EmptyCutoff"|Deprecated. Do not use.|/RMD|
EmptyCutoff [deprecated]
Deprecated. Do not use.

YieldFunctions

YieldFunctionsmemberhiddenYieldFunctions [toggle]

PathFindPathAsync ( Vector3 start, Vector3 finish )

Finds a path between the two provided points.

|RMD member="API:Class/PathfindingService/FindPathAsync"|Finds a path between the two provided points.|/RMD|
InstanceComputeRawPathAsync ( Vector3 start, Vector3 finish, float maxDistance ) [deprecated]

Deprecated. Do not use.

|RMD member="API:Class/PathfindingService/ComputeRawPathAsync"|Deprecated. Do not use.|/RMD|
InstanceComputeSmoothPathAsync ( Vector3 start, Vector3 finish, float maxDistance ) [deprecated]

Deprecated. Do not use.

|RMD member="API:Class/PathfindingService/ComputeSmoothPathAsync"|Deprecated. Do not use.|/RMD|
FindPathAsync ( Vector3 start, Vector3 finish )
Finds a path between the two provided points.
ComputeRawPathAsync ( Vector3 start, Vector3 finish, float maxDistance ) [deprecated]
Deprecated. Do not use.
ComputeSmoothPathAsync ( Vector3 start, Vector3 finish, float maxDistance ) [deprecated]
Deprecated. Do not use.


|RMD member="API:Class/PathfindingService"|PathfindingService is used to find a path between two points. The paths that the service finds are meant for regular sized Roblox characters. The paths make sure the characters can move between the points without running into walls or other obstacles. Paths will not only show where a character needs to walk to reach their goal, but will also show if the character will need to jump to get over gaps. Paths can be used both for characters controlled by players and for non-player characters. PathfindingService generates a navigation mesh over all of the parts in the place while a game is running. This mesh indicates where a regular sized character can walk. Any path that is created with the service will stay within the mesh. If the geometry of the place changes, for example if a part moves or is created, then the navigation mesh will be recaulculated. To see the navigation mesh for a place, open the place in Studio, navigate to File > Settings, and toggle on the setting Show Navigation Mesh. The mesh will then show up in the 3D view. The purple areas in the place show where a character can walk. The areas that are not colored in are considered blocked for characters. Studio also displays arrows on top of the mesh. These show where a character would have to jump in order to get from one part of the mesh to another. NavigationMesh.png PathfindingService has one function called FindPathAsync which is used to find paths. This function takes two parameters: the starting point of the path and the end point. This function then returns a Path object. To learn more about how the Path object works, please read this page. |/RMD|


Example

In this example, PathfindingService is used to find a path between two parts. The path is then drawn using LineHandleAdornments. The path will be colored in yellow for the segments where a character would need to jump.

The code used in this example can be seen in this Roblox place. The place is not copy-locked so it can be opened in Studio by anyone.

-- Make variables for Services
local PathfindingService = game:GetService("PathfindingService") 
-- Variables for the parts representing the start and end of the path
local start = game.Workspace.Start
local finish = game.Workspace.Finish
 
-- Calculate the path and get the waypoints
local path = PathfindingService:FindPathAsync(start.Position, finish.Position)local waypoints = path:GetWaypoints()
 
-- Cycle through all of the waypoints and draw a segment between each
for i = 2, #waypoints do
	-- Get the current and last waypoint in the path
	local lastWaypoint = waypoints[i - 1]
	local currentWaypoint = waypoints[i]
 
	-- Create a LineHandleAdornment. This adornment will be attached to the starting
	-- point part, so the CFrame of the adornment has to be calculated relative to
	-- the starting part.
	local segment = Instance.new("LineHandleAdornment")
	local lastPosition = lastWaypoint.Position + Vector3.new(0, 0.5, 0) - start.Position
	local currentPosition = currentWaypoint.Position + Vector3.new(0, 0.5, 0) - start.Position
	local toCurrent = currentPosition - lastPosition
	local distance = toCurrent.Magnitude	
 
	-- Style and attach the adornment
	segment.CFrame = CFrame.new(lastPosition, currentPosition)
	segment.Parent = start
	segment.Adornee = start
	segment.Thickness = 10
	segment.Length = distance
 
	-- Color the adornment based on the action of the waypoints. Green indicates walking,
	-- yellow indicates a jump.
	local waypointType = currentWaypoint.Action	
	if waypointType == Enum.PathWaypointAction.Jump then
		segment.Color3 = Color3.new(1, 1, 0)
	else
		segment.Color3 = Color3.new(0, 1, 0)
	end
end