If a player is looking at an NPC then it stops

Hello i need a script that will make it so when player is looking at an NPC it stops, when player is looking at an NPC for too long the NPC gets faster and can’t be stopped.

I’ve been trying to get this system from a few months and i couldn’t figure it out so i need your help programmers.

2 Likes

In a LocalScript, you can check if the part is in frame of the camera. To do that, use workspace.CurrentCamera:WorldToScreenPoint, it will return a Vector2 and a bool value. Then, by grabbing camera’s CFrame, you can make a raycast to see whether the part visibility is obstructed.

You can use the dot product of the camera and the NPC.
Here’s the local script:

local function looking()
    local CamLookVector = Camera.CFrame.LookVector
    local OgCFrame = NPC.HumanoidRootPart.CFrame

    NPC.HumanoidRootPart.CFrame = CFrame.LookAt(NPC.HumanoidRootPart.Position, Camera.Position)

    local NPCLookVector = NPC.HumanoidRootPart.CFrame.LookVector
    local dotproduct = CamLookVector:Dot(NPCLookVector)

    if dotproduct >= 0.6
        print('Camera is facing the NPC!')
        NPC.HumanoidRootPart.CFrame = OgCFrame
    end
end

while task.wait(0.1) do
    task.spawn(looking)
end