How can I make my enemy not clip into my walls?
I made this pathfinding script and it works but it likes to clip on walls, ive tried changing agent radius to many different values and I see no change in its behavior, other than setting it too high makes it unable to generate a path, ive also tried making the wall material cost be math.huge.
And ive tried making the hitbox smaller and larger.
Do I just have to make my own pathfinding script without the service?
Video: https://youtu.be/qryDlB4smfI
local Pathfinding = {}
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local path = PathfindingService:CreatePath({
AgentRadius = 1.5,
AgentHeight = 6,
AgentCanJump = false,
WaypointSpacing = 4,
Costs = {
Rubber = math.huge,
Carpet = 1,
Plaster = math.huge
}
})
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
function Pathfinding.followPath(AIcharacter,PlayerCharacter)
print('runig')
local target = PlayerCharacter:WaitForChild('HumanoidRootPart').Position
local humanoid = AIcharacter.Humanoid
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(AIcharacter.PrimaryPart.Position, target)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
-- Detect if path becomes blocked
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting path blockage until path is re-computed
blockedConnection:Disconnect()
-- Call function to re-compute new path
print("Blocked")
Pathfinding.followPath(target)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
-- Initially move to second waypoint (first waypoint is path start; skip it)
--print(waypoints)
nextWaypointIndex = 2
if waypoints[nextWaypointIndex] and waypoints[nextWaypointIndex].Position then
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
end
--print(waypoints)
else
warn("Path not computed!", errorMessage)
end
end
function Pathfinding.showPath()
for i,v in ipairs(game.Workspace:GetChildren()) do
if v.Name == "WaypointThis" then
v:Destroy()
end
end
for i,v in ipairs(waypoints) do
local part = Instance.new("Part")
part.Name = "WaypointThis"
part.Parent = game.Workspace
part.Size = Vector3.new(1,1,1)
part.Anchored = true
part.CanCollide = false
part.Position = v.Position
end
end
return Pathfinding