How do I remove the jitter from an animating, moving model?

In my tower defense game, I use parts for waypoints and then collect their positions to form a path using the BezierPath module. Using the path, I calculate the uniform CFrame at a point then set the model’s primary part there. Here’s the code:

function BaseMonster:UpdatePosition(dt)
	if not self.path or self.stunned then return end
	
    -- Lifting head so that the feet are not in the ground
	local boundingBoxCFrame, boundingBoxSize = self.model:GetBoundingBox()
	local bottomPosition = boundingBoxCFrame.Position - Vector3.new(0, boundingBoxSize.Y / 2, 0)
	local difference = self.model.PrimaryPart.Position - bottomPosition

	self.pathPosition = math.min(self.pathPosition + self.speed * dt, 1)
	local newPos = self.path:CalculateUniformCFrame(self.pathPosition) + difference
	self.model:SetPrimaryPartCFrame(newPos)

	if self.pathPosition >= 1 then
		self:ReachEndOfPath()
	end
end

This is done every time RunService.Stepped occurs and uses the deltaTime to determine how far to travel. So far, this works fine, other than the optimization issues. The main problem that I have using this method is the jitter of animations. Here’s what the animation is supposed to look like:

RobloxStudioBeta_2A1S5aTe5J

Here is what it ends up looking like:

RobloxStudioBeta_u9eOXK7lCU

I assume that the jitter comes from setting the CFrame of the primary part of the model, but I have no idea what other movement method to use that preserves animation.

Here’s the code that I used to set the animation (the script is a child of the enemy model for convenience at the moment):

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18298531920"

local rig = script.Parent
local humanoid = rig:FindFirstChildOfClass("Humanoid") or rig:FindFirstChildOfClass("AnimationController")

local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
	animator = Instance.new("Animator")
	animator.Parent = humanoid
end
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()

I’d love to hear of any alternatives to my current method because mine seems extremely flawed.

1 Like

Does this still occur if you only change the model’s position? I experienced a similar problem with CFrame transformations conflicting with animations.

IS it jittering on the server and the client or just one or the other, also are you performing the animating on the client or the server?

2 Likes

Do you have any example code on how to move a model? The only two methods of changing the position I know of are my current method and :MoveTo().

Both are done on the server. Should I have them separate? I’ve been told to spawn models and play animations on the client side, which I have not done since I performed a simple test.

Yeah this could be a contributing factor to the jitter, the client should be the ones doing the animating

1 Like

Thank you so much! Spawning the model on the server and animating it on the client works perfectly. However, I worry about optimization if I spawn many of these models since I’m creating a tower defense game where hundreds of enemies could be on the path. I tested the movement + animating on the client, which makes the model look like a robot (since the head cannot move).

RobloxStudioBeta_0v2k3TSXlr

2 Likes

Bumping this to see if anyone has a solution to allowing for head movement while spawning and animating moving models on the client side.

Could you show a video possibly?

Apologies, I should have mentioned it before. The one I posted earlier was actually an example of what I don’t want because the head does not move. In my original post, you can see how I want it to move (the first gif).