#NPC help

1 messages · Page 1 of 1 (latest)

median steppe
#

When my npc reaches its designated position, the player is able to walk behind it and avoid its attacks since its orientation doesnt automatically track the player. Using CFrames causes stuttering, so I was wondering if there is a way I can match npc orientation to the player orientation while avoiding the stuttering? Script:

while npctable.MachineState.Value == "chase" do
if not npctable.Target then return "FAIL" end
if not npctable.Path then break end
local TargetPos = TargetMidPoint.Position
local MidPos = MidPoint.Position
local direction = (TargetPos - MidPos).Unit
local movetoPos = TargetPos - direction * StopDistance

-- doing this instead of targetpos has led to some choppiness
-- print(Humanoid.WalkSpeed)
if not npctable.Target then return "FAIL" end
local distance = (MidPos - TargetPos).Magnitude
if distance < (attackrange - 3) and npctable.CanAttack then
    local Inversed = npctable.MidPoint.CFrame:Inverse() * TargetMidPoint.CFrame
    if Inversed.Z < 0.5 then
        return "SUCCESS"
    end
elseif distance > npctable.Range then
    return "FAIL"
end
if distance <= 8 then
    updatetime = 0.1
elseif distance >= 15 then
    updatetime = 0.35
end
if distance > 16 then
    if not TrackRun or time() - TrackRun > 3 then
        Replicator:CreateEffect("Speed", {Value = 6}):Debris(2)
        TrackRun = time()
    end
end
-- if Path.Status == Path.StatusType.Active then
--     Path:Stop()
-- end
if distance < attackrange then
    Humanoid:MoveTo(movetoPos)
else
    npctable.Path:Run(movetoPos)
end
local StartTick = tick()
repeat
    task.wait()
until ((tick() - StartTick) > updatetime)

end

willow gazelle
#

I'm not a genius on this but I believe you can dot product to see the way in which two CFrames are facing

#

Dot product returns a value from 1 to -1

#

1 meaning that the cframe is infront of the other cframe

#

0 meaning that the cframe is perpendicular/aligned (or in simpler terms) on the side of the other cframe

#

and lastly -1 meaning that the cframe is behind the other cframe

#

so for you system, you would want to check if the dot product of the player and npc is less than 0 meaning they are behind them (180 degrees) or you can trim it down to a dot product of let's say -0.1 (162 degrees) behind them just so that the fov of the npc mimics the one of a human in real life if that is what you are trying to achieve.