AI Can't Find Nearest Cover Point

Hello. My first time writing topics.
The bug is AI’s cover point finder, instead of searching nearest one, he searchs the furthest one. My AI uses Attachments named “CoverPoint”, that contains in a big part (NavZone). When AI gets nearest point, then raycasting from the Cover Point to the target, so AI can know is that actually hideable Cover Point.

Here’s the script:


-- It's a Module-Helper.

local Map      = workspace:WaitForChild("Map")
local NavMesh  = Map:WaitForChild("NavMesh")
local NavZone  = NavMesh:WaitForChild("NavZone") -- folder that contains all CoverPoints.

local a = {}

function a.GetNearestCoverPoint(Magnituder: BasePart)
	
	local CoverPointToReturn   = nil
	local Nearest_TO_MAGNITUDE = math.huge
	
	for _, NavPoint in ipairs(NavZone:GetChildren()) do
		
		if NavPoint.Name == "CoverPoint" and NavPoint:IsA("Attachment") then
			
			local Return = (Magnituder.Position - NavPoint.Position).Magnitude
			
			if Return < Nearest_TO_MAGNITUDE then -- if 'Return' smaller, then...
				CoverPointToReturn   = NavPoint
				Nearest_TO_MAGNITUDE = Return
			end
		end
		
		task.wait()
		
	end
	
	if CoverPointToReturn ~= nil then
		print("nearest is " .. CoverPointToReturn.Name .. ", with distance " .. Nearest_TO_MAGNITUDE)
	else
		print("no nearest cover points found")
	end
	
	if CoverPointToReturn ~= nil and CoverPointToReturn:IsA("Attachment") then
		CoverPointToReturn.Visible = true
	end
	
	return CoverPointToReturn
	
end

return a

Part of script which is communicates with Module-Helper:

function ActionScenario.CanCover()
		
		local Cover = AI_Helper.GetNearestCoverPoint(RootPart)
		
		if Cover and ActionScenario.GetDistance(Cover) <= DistanceForFindCov then
			DebugPrint("Cover: CanCover return = true")
			print("true")
			return true
		else
			DebugPrint("Cover: CanCover return = false")
			print("faelse")
			return false
		end
		
	end
	
	function ActionScenario.AttemptToCover()
		
		if EquippedWeapon and not MeleeDebounce and not TechnicallyDied and not CoverInProgress then
			
			print("attempt to cover hey")
			
			local CanCover = ActionScenario.CanCover()

			if CanCover and not CoverInProgress then

				CoverInProgress = true
				
				local NearestPoint = AI_Helper.GetNearestCoverPoint(RootPart)
				
				if NearestPoint then
					
					local CanEnemySeeHere = TargetScenario.CanSeeTarget(NearestPoint)
					
					if not CanEnemySeeHere then
						
						print("found cover")
						PathingScenario.PathTo(NearestPoint)
						CoverInProgress = true
						
					else
						
						print("moving to last cover for continue searching new one ruh")
						PathingScenario.PathTo(NearestPoint)
						CoverInProgress = false
						
					end
					
				else
					
					CoverInProgress = false
					
				end
				
			else
				
				CoverInProgress = false

			end
			
		end
		
	end

As actual AI’s code WAYYY too big i can’t paste it here.

As i hope everything in-code is self explanitory.
If something would be needed, reply!

Mistake was in a line:

Must be:

local Return = (Magnituder.Position - NavPoint.WorldPosition).Magnitude

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.