Enemy NPC jittery/ teleporting after landing

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    After digging up, my enemy npc starts jittering which I want to stop.
    also is my current system of moving an npc with while true do and lerp efficient?

My script works by lerping the npc from underground with sin to make it jump in an arc

function module.DigUp() – dig up function

local JumpTime = 2.5
local frames = JumpTime * 40 -- 40 lerp frames per second
local frameTime = 1 / frames
local landPos = HumanoidRootPart.Position + Vector3.new(0, 35, 0)
local yOffset = 50 -- how many studs Up i want the npc go to while jumping
local nearestPlayer, distance, direction = EnemyAIModule.findClosestFrom(HumanoidRootPart)
if nearestPlayer then
	local PlayerPos = nearestPlayer.Character.HumanoidRootPart.Position
	HumanoidRootPart.Position = Vector3.new(PlayerPos.X, HumanoidRootPart.Position.Y, PlayerPos.Z)
	landPos = EnemyAIModule.FindPlayerGroundPos(nearestPlayer.Character.HumanoidRootPart) -- this command uses raycast to find the ground position from the player
end
local explosionSize = 20
local Indicator = EnemyAIModule.CreateCircleIndicatorForPlayer(landPos, explosionSize)
local originPOS = HumanoidRootPart.Position
local accumDt = 0
local PercentAccum = 1
local Exploded = false
task.wait(2)

jumpUp:Play()
jumpUp:AdjustSpeed(2.35/JumpTime) -- 2.35 is the animation length


while true do -- lerps the character to move in an arc
	accumDt = math.min(accumDt + frameTime, JumpTime * 1) -- make accumDt go up to digTime

	local lerpedPos = originPOS:Lerp(landPos + Vector3.new(0, 10,0), accumDt) -- divide accumDt by digTime to make the lerp time scale with digTime
	local lerpedPosY = lerpedPos.Y -- modify Y position to not make it go straight like X and Z axis
	local slerpYOffset = math.sin(math.pi * accumDt) * yOffset
	local newCFRAME = CFrame.new(lerpedPos.X, lerpedPos.Y + slerpYOffset, lerpedPos.Z)
	CarrotMonster:PivotTo(newCFRAME)
	if HumanoidRootPart.Position.Y >= landPos.Y and HumanoidRootPart.Position.Y <= landPos.Y + 3 and not Exploded then
		Exploded = true
		Indicator:Destroy()
		module.DigUpExplosion(HumanoidRootPart.Position, explosionSize) -- not relevant to prompt
	end

	if accumDt >= PercentAccum then
		HumanoidRootPart.Anchored = false
		break
	end

	task.wait(frameTime * JumpTime)
end

end

Does the npc normally do this (without the dig animation thing) or does it only happen after the dig animation? | Follow up; if the npc does normally do that, maybe its not welded correctly to the HumanoidRootPart

Found the solution. A previous function that made the enemy dig down instead of dig up made the enemy moving downwards by loop setting HumanoidRootPart.Position and not by using CarrotMonster:PivotTo(Cframe). apparently setting HRP.Position made the server see the enemy differently from client??? but it solved the problem

1 Like

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