Hi guys, I am trying to use Humanoid:MoveTo() and Model:PivotTo() to move my NPC to the enemy to attack it. PivotTo() runs every frame once in attack range where MoveTo() will be stopped using
function ChaseTarget.OnExit(unit)
unit.Humanoid:MoveTo(unit.Model:GetPivot().Position)
end
in the video, on client side you can see that my NPC snaps onto the position before attacking while on server side it arrives smoothly in attack range before attacking. How can I get it to behave like the server sided one? I have tried setting network ownership of the Head or HumanoidRootPart of the NPC model to myself but the issue is still the same.
my attack update loop:
function Attack.Update(unit)
local target = unit.Target
if not target then
unit.StateMachine:SetState("Idle")
return
end
local targetPosition = target.Model:GetPivot().Position
local unitPosition = unit.Model:GetPivot().Position
if (targetPosition - unitPosition).Magnitude > unit.AttackRange then
unit.StateMachine:SetState("ChaseTarget")
return
end
-- face the enemy
local hrp = unit.Model:FindFirstChild("Head")
if hrp then
local direction = (targetPosition - unitPosition).Unit
local yaw = math.atan2(-direction.X, -direction.Z)
local targetCFrame = CFrame.new(hrp.Position) * CFrame.Angles(0, yaw, 0)
hrp.CFrame = targetCFrame
end
if unit.AttackReady then
unit.AttackReady = false
local animator = unit.Humanoid.Animator
animator:LoadAnimation(animator.Attack):Play()
task.delay(unit.AttackRate, function()
if unit then
unit.AttackReady = true
end
end)
end
end