Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect which direction an npc is walking?

Asked by 2 years ago

I'm working with an NPC and am trying to enable/disable some billboard guis when it walks in certain directions. Nothing happens when I try this with the npc but it does work when I try this with the player

local camera = game.Workspace.CurrentCamera
local humanoid = script.Parent:FindFirstChild("Humanoid")
local pb1 = game.Workspace.Dummy:WaitForChild("Bag").pb1
local pb2 = game.Workspace.Dummy:WaitForChild("Bag").pb2

function Test(disabled)
    if (humanoid.MoveDirection:Dot(camera.CFrame.RightVector) > 0) then --when the npc runs upwards or to the right, pb2 is no longer visible and pb1 becomes visible.
        pb1.Enabled = true 
        pb2.Enabled = false
    elseif (humanoid.MoveDirection:Dot(camera.CFrame.RightVector) < 0) then ----when the npc runs downwards or left, pb1 is no longer visible and pb2 becomes visible.
        pb1.Enabled = false 
        pb2.Enabled = true
    end
end

humanoid.Running:Connect(Test)

(not sure if this method is for the player's humanoid only)

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

Use Humanoid.RootPart.AssemblyLinearVelocity.Unit or something. It'll get the relative unit to 1 stud.

Ad

Answer this question