don't mind this anymore, i got this solved a while ago, but thanks to anyone who even looked at it!

Hi there!

I am currently working on a field of view script for an enemy npc. After researching on this a bit and looking at a few examples i used dot product and this is what I have so far.


function inFov(enemy, playerChar)
	if enemy and playerChar then
		local view_distance = 1000
		local pos = playerChar:findFirstChild("HumanoidRootPart")
		if playerChar and pos then
			local direction = ((pos.Position - enemy["Head"].Position).unit)
			if direction.magnitude <= view_distance then
				local dot = direction:Dot(enemy["Head"].Position) -- CFrame.lookVector might be better.
				if dot > 0 then
					print ("In Sight")
					return playerChar.Name
				end
			end
		end
	end
end

As you can see it's a function. I call it through the main part of the code which is a loop that loops through all the players in the game. It basically checks if the dot product of distance is more than 0 and if so it determines if the enemy can see the player. However this isn't very efficient and I want it to check if the player is within a certain angle of the npc head's facing direction. I looked up some tutorials online because I'm still very new to dot product, but becuase most of them were in different scripting languages and game platforms, i needed to convert it to work with Lua and Roblox studio. Unfortunately that did not go very well, along with the other few things I tried.

Here are some links to the Wiki and the Youtube video I watched if anyone is interested.

The Wiki article is a bit complex and it might be that I'm just missing a small piece of the puzzle, but if anyone could help me achieve a proper field of view I would greatly appreciate it! Thanks in advance!

2 comments

0

Ah, I saw that already but its used for the player's camera. though i could try to make something out of it