How to make a part move when a player isn't looking at it

Hello, I want to make a part move when the player isn’t looking at it. My game is a first-person game and I want to make it so if the character or player’s camera isn’t looking at it, the part will move to the player and kill them. Just that I have no idea how I would code this. I just need a way to get it if the player isn’t looking at the part then it will move.

1 Like

Create a raycast from the player’s orientation and position and make the part move if the raycast doesn’t hit the part.

1 Like

I will try making a raycast from the player’s head to see if they are looking at it. If not the make the part move to the player

1 Like

oh, that’s simple!
make a module script in the replicated storage and put this inside the module script:

local module = {target = function(part,player)
	if player.Character or player.CharacterAdded:Wait() then
	local ti = 3 --how long you want the part to take to reach the player
	local pos, tr = workspace.CurrentCamera:WorldToViewportPoint(part.Position)
		if tr == true then
		local tween = game:GetService("TweenService")
	tween:Create(part,TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Position = player.Character.UpperTorso}):Play()
		end
		end
	end}

return module

and you can call this from a local script to hit a player
eg:

local ms = require(game.ReplicatedStorage.ModuleScript)
local part = game.Workspace.Part --change this to whichever part you want
local player = game.Players.LocalPlayer --change this to whichever player instance you want
ms.target(part,player)

Or it’ll be easier if you use an rocket propulsion instance, I’d suggest trying that out since it’s easier and more efficient!

Edit: I didn’t notice that you wanted to check if the player’s looking at the part,
you can accomplish that easily without raycasting by using “worldtoviewportpoint”
I edited the module to check it for you!

2 Likes

I want the part to stop moving if the player starts looking at it again. And this looks like it’ll fire once then not fire again.

Ohhh, then let’s scratch off the module idea!

now let’s just control it with one local script:

local run = game:GetService("RunService")
local part = game.Workspace:WaitForChild("Part") --change this to any part you want
local rp = Instance.new("RocketPropulsion",part)
local plr = game.Players.LocalPlayer

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
	rp.Target = char:WaitForChild("UpperTorso")
	part.Anchored = false
end)

run.RenderStepped:Connect(function()
	if plr.Character or plr.CharacterAdded:Wait() then
		local pos,tr = workspace.CurrentCamera:WorldToViewportPoint(part.Position)
		if tr == true then
			rp.Target = nil
			part.Anchored = true
		else
			rp.Target = plr.Character.UpperTorso
			part.Anchored = false
		end
	end
end)
1 Like

Could you give me an explanation on what RocketPropulsion does? I do not know anything about body movers.

Rocket propulsion just moves it’s parent part towards another part (target), it works just like how you would expect a missile to work!

While the code does check if the player is looking at the part, the part doesn’t move at all

Ohh, did you try the script in a local script under starter player scripts?

Yes I have the localscript in the StarterCharacterScripts. I’ll try moving it to the StarterPlayerScripts

Nope. Same result, the part unanchors but doesn’t move at all

local run = game:GetService("RunService")
local part = game.Workspace:WaitForChild("Part") --change this to any part you want
local rp = Instance.new("RocketPropulsion",part)
local plr = game.Players.LocalPlayer

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
	rp.Target = char:WaitForChild("UpperTorso")
	part.Anchored = false
end)

run.RenderStepped:Connect(function()
	if plr.Character or plr.CharacterAdded:Wait() then
		local pos,tr = workspace.CurrentCamera:WorldToViewportPoint(part.Position)
		if tr == true then
			rp.Target = nil
if part.Anchored == false then
print("player looking")
end
			part.Anchored = true
		else
			rp.Target = plr.Character.UpperTorso
                       f part.Anchored == true then
print("player not looking")
end
		part.Anchored = false
		end
	end
end)

Can you try this out and send me a ss of the output?

image

Give me a sec, lemme try it out myself as well!

I have found a solution to this. Instead of rocket propulsion, I used CFrame to teleport the block behind the player, which is what I wanted.

-- Using a portion of PhAnToMjose4605's code

local run = game:GetService("RunService")
local part = workspace:WaitForChild("Blark") --change this to any part you want
local plr = game.Players.LocalPlayer

run.RenderStepped:Connect(function()
	if plr.Character or plr.CharacterAdded:Wait() then
		local pos,tr = workspace.CurrentCamera:WorldToViewportPoint(part.Position)
		if not tr then
			part.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,3) -- Move part behind player
		end
	end
end)
2 Likes

Ummm assuming that you’re creating SCP-173 The workspace.CurrentCamera:WorldToViewportPoint won’t account for Obstruction so I highly advise you to also check that you can actually see the NPC by raycasting from the camera CFrame