Creating an NPC that moves when you look away

Hi all!
I’m working on my horror game called “Neighbourhood Watchers” where basically I’m trying to get the guy below to only move towards you when you aren’t looking at it but does not move when it is in your POV. Very similar to the ones in “A Reeally Fricking Big House” or “Identity Fraud”. (my monster is not based off of IF even though it may look like the ones there)
image
I saw a topic about this and tried the script below but it did not work.

local Position, PlayerCanSee = Camera:WorldToScreenPoint(part.Position)
if PlayerCanSee then
-- Do something with our model.
end
end)

Any help would be very much appreciated, thanks!

1 Like

There is a ViewportSize property describing the client’s screen dimensions.

By comparing the 2D screen position, you can see if this position is within range of those dimensions.

1 Like

Try using BodyGiro
And vector3

fpor = game.Workspace.FindPartOnRay
seen_dist = 50 --distance u want it to detect 
function canSee(subject,viewer)
	if (not subject) or (not viewer) then return false end
	local sh = subject:findFirstChild("Death")
	local vh = viewer:findFirstChild("Head")
	if (not sh) or (not vh) then return false end
	local vec = sh.Position - vh.Position
	local isInFOV = (vec:Dot(vh.CFrame.lookVector) > 0)
	if (isInFOV) and (vec.magnitude < seen_dist) then
		local ray = Ray.new(vh.Position,vec.unit*200)
		local por = fpor(workspace,ray,viewer,false)
		return (por == nil) or (por:IsDescendantOf(subject))
	end
	return false
end
function canSee2(subject,viewer)
	if (not subject) or (not viewer) then return false end
	local sh = subject:findFirstChild("Death")
	local vh = viewer:findFirstChild("Head")
	if (not sh) or (not vh) then return false end
	local vec = sh.Position - vh.Position
	if (vec.magnitude < seen_dist) then
		local ray = Ray.new(vh.Position,vec.unit*200)
		local por = fpor(workspace,ray,viewer,false)
		return (por == nil) or (por:IsDescendantOf(subject))
	end
while true do
	local minmag = nil
	local minply = nil
	local mindir = nil
	local beingwatched = false
	players = game:GetService("Players"):GetChildren()
	for i=1,#players do
		char = players[i].Character
		if char then
			local foundhead = char:FindFirstChild("Head")
			local foundHumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
			local foundhum = char:FindFirstChild("Humanoid")
			if foundhead and foundHumanoidRootPart and foundhum and foundhum.Health > 0 then
				local sub = (script.Parent.CFrame.p - foundhead.CFrame.p)
				local dir = sub.unit
				local mag = sub.magnitude
				if not minmag or minmag > mag then
					minmag = mag
					minply = char:FindFirstChild("HumanoidRootPart")
					mindir = dir
					if canSee(script.Parent.Parent, char) then beingwatched = true end
				end
			end
		end
	end

	if minply and not beingwatched and canSee2(script.Parent.Parent, minply.Parent) then
		if minmag and minmag <= 200 then
			local unit = (script.Parent.Position-minply.Position).unit
			unit = Vector3.new(unit.X,0,unit.Z)
			script.Parent.CFrame = CFrame.new(script.Parent.Position + (unit*-15), Vector3.new(minply.Position.X, script.Parent.Position.Y, minply.Position.Z))
			script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(180),0)
			script.Parent.Slide:Play()
			wait(0.000001)
			if minmag < 10 and minply.Parent:FindFirstChild("Humanoid") and minply.Parent.Humanoid.Health > 0 and not beingwatched then
				script.Parent.CFrame = CFrame.new(script.Parent.Position, Vector3.new(minply.Position.X, script.Parent.Position.Y, minply.Position.Z))
				minply.Parent:BreakJoints()
			end
		end
	end
2 Likes