NPC teleports and choppy movement when rotation is set

Hey! I have a little bit of a problem where my NPC is doing some weird movement when I set his rotation and sometimes he even teleports. What I wanna achive is my character to walk smooth and stop when in range. When it’s in range it should look in the direction of it’s target and attack it.

Here is a gyazo gif of when I try to set it’s rotation → https://gyazo.com/970ca0e67e9dee45c7dc213e49c7edbe

And here is a gyazo gif without the rotation fix → https://gyazo.com/0d09497d5d5644d979bfb03a70e88706

local pathFindingService = game:GetService("PathfindingService")
local combatModule = require(game.ServerScriptService.MainModule.CombatModule)

local figure = script.Parent
local player = script.Parent.Owner.Value
local spot = figure.Spot.Value

local agro = player.Agro
local target = player.Target

local ProbablyStuck = false
local JumpDebounce = false

local attackDebounce = false

while true do
	
	local attackDamge = figure.Damage.Value
	local attackRange = figure.Range.Value
	local attackCooldown = figure.Cooldown.Value
	
	if player.Character then
		
		local ForwardSpot = spot.CFrame * CFrame.new(0,0,-8)
		
		local Distance = (ForwardSpot.Position - figure.HumanoidRootPart.Position).Magnitude
		
		if player.Character.Humanoid.MoveDirection.Magnitude == 0 then

			if agro.Value == true and target.Value ~= nil then
				
				if not target.Value:FindFirstChild("RootPart") then
					
				else
					
					local TargetDistance = (target.Value.RootPart.Position - figure.HumanoidRootPart.Position).Magnitude

					if TargetDistance < attackRange then

						if math.floor(figure.HumanoidRootPart.Velocity.Magnitude) > 0 then
								
							figure.Humanoid:MoveTo(figure.HumanoidRootPart.Position)
							
							local npcPosition = figure:GetPivot().Position
							local enemyPosition = target.Value:GetPivot().Position
							local direction = (enemyPosition - npcPosition).Unit
							local lookAtCFrame = CFrame.lookAt(npcPosition, enemyPosition)
							figure:PivotTo(lookAtCFrame)

						else

							task.spawn(function()

								if not attackDebounce then

									attackDebounce = true

									combatModule.Hit(player, target.Value, attackDamge, "Basic")
										
									task.wait(attackCooldown)

									attackDebounce = false

								end

							end)

						end

					else

						figure.Humanoid.WalkSpeed = 16

						figure.Humanoid:MoveTo(target.Value.RootPart.Position)

					end
					
				end

			else
				
				figure.Humanoid:MoveTo(spot.Position)

			end
1 Like

A really simple way to do this is editing the root C0 to make it so that the torso looks at the crate through local side (for optimization), thus making it very jitter-free. I hope this information helps.

The problem was that I changed the rotation of the NPC while it was moving, so I fixed it by checking if the NPC was standing still and then rotating.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.