Heya there.
From a previous thread I made yesterday ago, someone told me to modify a pathfinding module to make it so that it’ll make enemies constantly raycast around 1/5th of a second. If the ray hits something other than themselves, the players, or the objective, they must’ve detected a wall and will pathfind.
I’m doing this so that I won’t need to constantly pathfind and only do so if necessary. This was because the pathfinding seemed to break as more and more enemies spawned in.
As the title says, the RaycastResults are flipped for whatever reason. I’m wondering as to why it is. (Honestly, it’s a simple fix, just flip them. But I’m curious as to why.)
--//This is a small snippet of the pathfinding module, containing the raycasting code.
--//Preperation
local Objective = game.Workspace:FindFirstChild("Crystal")
--Enemy
local RootPart = NPC.PrimaryPart
local Humanoid = NPC.Humanoid
local CharacterSize = NPC:GetExtentsSize()
RootPart:SetNetworkOwner(nil)
--//Pathfinding Agents
local PathfindingAgents = {
AgentRadius = (CharacterSize.X + CharacterSize.Z)/4,
AgentHeight = CharacterSize.Y,
AgentCanJump = true
}
--[[PATHFINDING]]--
local LookDirection = NPC.Head.CFrame.LookVector
local function FollowPath(Destination)
local RaycastingParams = RaycastParams.new()
RaycastingParams.FilterDescendantsInstances = {NPC, Objective}
RaycastingParams.FilterType = Enum.RaycastFilterType.Exclude
local RaycastResult = workspace:Raycast(NPC.Head.Position, LookDirection * 30, RaycastingParams)
if RaycastResult then
print("coding is hell.")
elseif not RaycastResult then
--//Let's assume it didn't hit a wall. That's good.
Humanoid:MoveTo(Objective.PrimaryPart.Position)
end
--[[
local NewPath = PathfindingService:CreatePath(PathfindingAgents)
NewPath:ComputeAsync(RootPart.Position, Destination)
if NewPath.Status == Enum.PathStatus.Success then
local Waypoints = NewPath:GetWaypoints()
for _, Waypoint in ipairs(Waypoints) do
if Waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
--Just in case
NewPath.Blocked:Connect(function(blockedWaypointIdx)
print("The enemy's path is blocked. Recalculating.")
if Objective and Objective.PrimaryPart then
FollowPath(Objective.PrimaryPart.Position)
end
end)
Humanoid:MoveTo(Waypoint.Position)
local Timer = Humanoid.MoveToFinished:Wait(1)
if not Timer then
print("Path timed out. Recalculating.")
if Objective and Objective.PrimaryPart then
FollowPath(Objective.PrimaryPart.Position)
end
end
end
else
print("Path failed. Recalculating. Sorry.")
end
]]
end