Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

A* Pathfinding algorithm seems to not work for bigger torso?

i am trying to make an pathfinding npc but this is what i see if i put a normal sized torso it avoids walls but when i make the torso a bit bigger it seems to not avoid the wall

My code for roblox pathfinding:

local pathfindingService = game:GetService("PathfindingService")
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.Destination.Position
local path = pathfindingService:CreatePath()
path:ComputeAsync(body.Position, destination)
local waypoints = path:GetWaypoints()
for k, waypoint in pairs(waypoints) do
	humanoid:MoveTo(waypoint.Position)
	if waypoint.Action == Enum.PathWaypointAction.Jump then
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
	humanoid.MoveToFinished:Wait()
end

1 comment

0

you are calculating based on the bodies position, not the size. Roblox's path finding service usually accounts for this I recommend using that. If you want to full account for size then use raycasting. You can also disable collisions for the wall and let the body move through it.

1 answer

✓ Accepted answer

The reason your pathfinding script is not working with a bigger torso is that Roblox's pathfinding service defaults for a normal character, which is 5 studs tall, about 3 studs in width. What you need to do is to add in path parameters.

local pathfindingService = game:GetService("PathfindingService")
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.Destination.Position


local pathParams = {
	["AgentHeight"] = 5, --The height of the character
	["AgentRadius"] = 2, --The width of the character from end of left arm to right arm times half.
	["AgentCanJump"] = true --Can the character jump?
}

local path = game:GetService("PathfindingService"):CreatePath(pathParams)

path:ComputeAsync(body.Position, destination)
local waypoints = path:GetWaypoints()
for k, waypoint in pairs(waypoints) do
    humanoid:MoveTo(waypoint.Position)
    if waypoint.Action == Enum.PathWaypointAction.Jump then
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    humanoid.MoveToFinished:Wait()
end

In the path params, you would need to adjust the values according to the size of your character.

1 comment

0

Thankyou @QWJKZXF

This discussion is preserved as it was. New answers, comments, and votes are closed.