Solo dev here. I am having trouble with setting up a hostile with low visibility. I have the code for the vision right here:
local npc = script.Parent
local visionCone = npc:WaitForChild("VisionCone")
local detectionRange = 30 -- Adjust the range as needed
local visionAngle = 45 -- Angle in degrees for field of view
local function isPlayerInVision(player)
local character = player.Character
if not character then return false end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return false end
local npcPosition = npc.PrimaryPart.Position
local playerPosition = humanoidRootPart.Position
local directionToPlayer = (playerPosition - npcPosition).unit
local npcFacingDirection = npc.PrimaryPart.CFrame.LookVector
local dotProduct = directionToPlayer:Dot(npcFacingDirection)
local angle = math.acos(dotProduct) * (180 / math.pi)
if angle < visionAngle and (playerPosition - npcPosition).magnitude < detectionRange then
return true
end
return false
end
local function onHeartbeat()
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if isPlayerInVision(player) then
print(player.Name .. " is in vision!")
end
end
end
game:GetService("RunService").Heartbeat:Connect(onHeartbeat)
But I cannot seem to code to make the hostile walk to the player and fight them. I have been looking through the dev forum for hours and I cannot find anything. Thank you if you help!