Heya there.
I’m working on a pathfinding module for my game that uses both the pathfinding service and raycasting. How the module works is simple, it’ll simply tell an enemy to constantly check the angle between itself, a player, and the objective. If the degree is greater than 90, than target the player. Otherwise, target the objective.
(Diagram for reference, I guess.)
Anyway, I’m stuck on the raycasting part (Below the for _, Players in pairs
loop in the FindNearestPlayer function) since I don’t know how I would go doing this. Should I constantly create two rays at the same time, essentially somewhat of a field of view thingy or should I use magnitude and all those other stuff.
I do have a basic understanding on raycasting, so don’t worry about teaching me what raycasting is.
--[[SERVICES]]--
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
--[[MODULE]]--
local PathfindingModule = {}
function PathfindingModule.Pathfind(NPC, AggroRange)
--//Preperation Variables
local Rootpart = NPC.PrimaryPart
local Humanoid = NPC:FindFirstChildWhichIsA("Humanoid")
local CharacterSize = NPC:GetExtentSize()
local Target = workspace:WaitForChild("Crystal")
local RunServiceFunc
--//PathfindingAgents
local PathfindingAgents = {
AgentRadius = (CharacterSize.X + CharacterSize.Z)/4,
AgentHeight = CharacterSize.Y,
AgentCanJump = true
}
local function FollowPath(Destination)
local Path = PathfindingService:CreatePath(PathfindingAgents)
Path:ComputeAsync(Rootpart.Positiom, Destination)
if Path.Status == Enum.PathStatus.Success then
local Waypoints = Path:GetWaypoints()
local Markers = {}
for _, Waypoint in pairs(Waypoints) do
if Waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid:MoveTo(Waypoint.Position)
Humanoid.MoveToFinished:Wait(2)
end
for _, Maker in pairs(Markers) do
Maker:Destroy()
end
else
warn("NPC got stupid and forgot how to pathfind, sorry!")
end
end
local function FindNearestPlayer()
local NearestPlayer = nil
local ClosestDistance = AggroRange
local PlayerList = Players:GetPlayers()
RunServiceFunc = RunService.Stepped:Connect(function()
for _, Player in pairs(PlayerList) do
--//Character Variables
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local Rootpart = Character.PrimaryPart
--Confused on this part now.
end
end)
end
RunService.Heartbeat:Connect(function()
--TBA
end)
end
return PathfindingService