R15 NPC looking at you script is half-working [Client-Side]

Hey scripters! I am trying to make a NPC that faces you when you are standing in front of him.

I am trying to re-make this from bubblegum simulator :

https://gyazo.com/f952f80e55e1b359144090ea9825cdad

Here’s my LocalScript code:

local RunService = game:GetService("RunService")
local NPC = game.Workspace.PoliceOfficerNPC
local head = NPC:WaitForChild("Head")
local neck = head.Neck
local waist = NPC:WaitForChild("UpperTorso").Waist
local HRP = NPC:WaitForChild("HumanoidRootPart")
local defaultc = neck.C1
local defaultc2 = waist.C1

local function onRenderStep()
    if game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then
        local hrp = game.Players.LocalPlayer.Character.HumanoidRootPart
        
        local targDirection = (hrp.Position - head.Position)
        targDirection = Vector3.new(targDirection.X, 0, targDirection.Z).unit
        
        local currentDirection = HRP.CFrame.lookVector
        currentDirection = Vector3.new(currentDirection.X, 0, currentDirection.Z).unit
        
        local current = math.atan2(-currentDirection.Z, currentDirection.X)
        local targ = math.atan2(-targDirection.Z, targDirection.X)
        
        if current < 0 then
            current = (2*math.pi) + current
        end
        if targ < 0 then
            targ = (2*math.pi) + targ
        end
        
        local result = current - targ
        if result > math.pi/2 or result < math.pi/-2 then
            result = 0
        end
        
        neck.C1 = neck.C1:Lerp(defaultc * CFrame.Angles(0, result/2, 0), 0.1)
        waist.C1 = waist.C1:Lerp(defaultc2 * CFrame.Angles(0, result/2, 0), 0.1) 
    end
end

RunService.RenderStepped:Connect(onRenderStep)

This is the result:

https://gyazo.com/9011c99b86464fa0b65fd16f2de233a9

The NPC faces me when I am on his right side but not on the left. Any clues on why that is? I can’t find another solution other than changing the NPC’s orientation.

Thanks for any help!

1 Like

Would you mind providing me with the NPC’s primary part orientation? I am unable to reproduce this issue in Studio on my own.


I don’t exactly understand your request. Basically what I meant is that when you rotate the NPC, it fully works but it doesn’t when it is rotated to a certain orientation.

I am asking what the orientation of the primary part is so I am able to reproduce the issue in studio.

image

{0,-33.93,0}

1 Like